A blog about my rantings, including Games, Game Development, Gaming, Consoles, PC Gaming, Role Playing Games, People, Gaming tips & cheats, Game Programming and a plethora of other stuff.
Thursday, 30 January 2014
glfw v3 on Debian/Mint/Ubuntu
Please Subscribe now to help me reach 1000 subs :)
This is a post just for me really, but if it helps others, so be it...
Installing glfw on Linux (Mint/Ubuntu/Debian)... My steps...
sudo apt-get install libx11-dev libgl1-mesa-dev libglu1-mesa-dev -libxrandr-dev libxext-dev
sudo apt-get install cmake xorg-dev
Download glfw code, and extract... Move to that folder...
sudo cmake -G "Unix Makefiles" -DBUILD_SHARED_LIBS=on -DCMAKE_INSTALL_PREFIX=/usr
sudo make
sudo make install
Then lets say we want to build the "boing" example, move to the examples folder with the boing.c file...
gcc -Wall -g -c ./boing.c -o obj/boing.o
g++ -o bin/Boing obj/boing.o -lGLU -lGL -lm -lX11 -lpthread -lXxf86vm -lglfw
So in Code::Blocks, your linker settings look like this for a glfw project:
Tuesday, 28 January 2014
String Replace in C# & Java
I hate the way an object orientated language like C# can completely ignore object orientation...
My example for the day... The Replace function in the String object....
string l_name = "Job";
l_name.Replace ("b", "g");
I don't know if it's just my Pascal lathered mind, or today in general, but this got really on my manboobs, that it did nothing... Because instead of the Replace function acting on the "l_name" object, it acted like a static function outside the string... Meaning I had to do this:
l_name = l_name.Replace("b", "g");
So I may as well have used C notation...
Replace (&l_name, "b", "g");
It's Functional rather than object orientated, I wanted "Replace" just to act on the object it was pointing at, to do "this->Replace" and not need to assign back to anything... That's how I'd have designed this function. And yes, if people wanted to assign a Replace leaving the original alone I'd have let them, like this:
class string
{
public:
void Replace (string& p_What, string& p_With);
static string Replace (string& p_Source, string& p_What, string& p_With);
};
Gah, object orientation is the point of C# existing... I note Java handles its string class the same way, and I find it so very very annoying...
And worse than finding it annoying me, I don't know quite why its annoying me so much, I think today I feel on the whole to be totally out of control of my own life... Not least because I note the company has docked £372 off of my pay for the xmas break, new rules in my new position... So already earning a pittence and running around after everyone else, I'm getting docked cash for time off at Christmas... Thanks Scrooge... Thanks.
Labels:
annoyance,
brain,
C#,
Code,
code patterns,
Java,
object orientated,
objects,
programming
Monday, 27 January 2014
Mr Xelous Mailing....
I've just done something which even I think is strange, I've just sent an e-mail to a person, at their place of work, directly asking for a job.
I have never done this before, I've had jobs applied for from adverts, I've gone into trade shows and been approached and head-hunted by others... But I've never just sent a message or cold called someone.
I'm pretty sure if I were receiving such messages - as I'm sure this person is - they'd just have them spam filtered, my mail is just spam, but I have a small warm spot deep inside hoping the recipient reads the mail and I hear something back.
It is a very small hope in this yammering, chattering, gnashing world, but hey you never know...
The reason for my sending this missive is three fold, first I think I need a new job... second I'd like to work for them... and third my personal research/coding time is very much pointed in the direction they appear to me moving, least from their current published information, they're looking at C++, I'm looking at C++... They have a history with volume rendering, I want a history with volume rendering... They have a company, I'd like to work for a company....
I'd just like to earn a little more than I do now, to live comfortably and work from home doing something I love. With a house with two man-lab rooms all to myself, I think it maybe time I try to make the most of it, the most of myself.
And hey if the recipient of that mail is here.. "Hi"... or should that be "Hej"?
Saturday, 25 January 2014
Miscellaneous January 2014
First off, I've been a right moody bastard tonight... I've had two goes at the wife, and though I'm sorry for both, I'm still an interminable bastard.
Second, I've finished the plastering... My skill since the first walls have increased extraordinarily, and the last wall I did is actually quite good!
Third, I've played some "Endless Space"... enjoying it, learning the tech tree...
And finally... Volume rendering... This is like Voxel rendering, as one see's in Minecraft, I'm a big fan of minecraft, I'm amazed Java manages to perform so well, so... I may just start a C++ project to learn a bit about this, the real reason being to use my OpenGL skills, and perhaps update them, the last real 3D programming I did was around 2000-2002 when I used it to render shop floor plans for a project at work, since then I've done a shit load (and I mean a shit load) of 2D programming, but I've not managed to get back into 3D.
So, 3D may be my private time research item.
Second, I've finished the plastering... My skill since the first walls have increased extraordinarily, and the last wall I did is actually quite good!
Third, I've played some "Endless Space"... enjoying it, learning the tech tree...
And finally... Volume rendering... This is like Voxel rendering, as one see's in Minecraft, I'm a big fan of minecraft, I'm amazed Java manages to perform so well, so... I may just start a C++ project to learn a bit about this, the real reason being to use my OpenGL skills, and perhaps update them, the last real 3D programming I did was around 2000-2002 when I used it to render shop floor plans for a project at work, since then I've done a shit load (and I mean a shit load) of 2D programming, but I've not managed to get back into 3D.
So, 3D may be my private time research item.
Labels:
annoying,
decorating,
gaming,
me,
Minecraft,
People,
programming
Friday, 24 January 2014
No Rolf.... Sexual Court Case Equality
Its interesting they don't mention Rolf Harris....
Now, he's in court on some abhorrent charges, but in this country you are innocent until proven guilty, and guilty or not Rolf Harris was part of UK culture linking us to Australia for decades, one can't simply change the list of Aussies in the UK because one is in court...
This is a little like mentioning iconic German leaders and leaving Hitler out cus he was... well Hitler.
And I know mentioning Nazi's derails my point, it always will, but the case against Rolf Harris is still on going, his name is mud either until the end of time, if he goes down, or he's exonerated by being acquitted, either way we simply don't know at this moment... But the media stance, and the naming & shaming is interesting.
I don't believe that these court cases about kiddy fiddling and sexual charges against the likes of Harris, or DLT, or Bill Roache come up for no reason. I think some are genuine, some are money grubbing and some are delusions seeking attention, but I think some of the cases where the accused are named & shamed before their day in court, like Michael LeVell, Jim Davidson & Fredie Star only for them to be found not guilty don't result in their names being restored.
Indeed I feel in the latter case where the accuser maybe found to be guilty of lying there should be a case against them and they should be named and shamed, its perhaps not as cut and dry as this, I personally have experience of a situation where two sisters were sexually assaulted from the ages of 3-5 to their early teens by their step-father and when it came to court the step-father was found not guilty, but you can see in those girls (well women now, they're both in their 30's and early 40's) the psychological shadow of the past and also the shadow that he got away with it.
I'm no legal expert, I do think I'm a bit of a common-sense expert, and so I think maybe total anonymity should be imposed, on the case, the persons and the press. And breaking this before the conclusion of a case in court should be contempt of court.
Otherwise lives maybe ruined, both of real victims, to falsely accused defendants to real abusers and rapists. (Of course the last case is fine, ruin them all, but the former two categories I think out weigh the latter in the current atmosphere of the press).
Thursday, 23 January 2014
Network Guru Fails at Monitoring his own Bandwidth
Do I come on here to bemoan the seeming incompetence of others a lot?... Because I have just had an annoying 20 minutes talking to one of our "Network Experts", there's a server box running Ubuntu, its actually a virtual machine, and it was being blamed for crippling the network on the host machine - which has to run several other Windows Server 2008 Virtual Machines.
For some reason, this came across my desk, because someone has decided that though I can't advise the company on Linux server usage or adoption, I can be called into sort out the problems other fools create.
So, this server was running, the administrator chap is sat there with PuTTY connected to it and he's saying there's too much network traffic coming from the box.
Looking at the process list the machine is doing nothing, when I ask for one of its users to be made to use it, the CPU usage is a time flitter and then the usage is over, it appears to me the Virtual machine host is waking the VM, using it, and closing it.
The processes being used are simple webservices, so there should be no network traffic unless someone is using the server.
So, I ask.. "What's your idea that this machine is causing the issue?" Very smugly the admin presented me with an A4 sheet with two graphs, one shows all his servers running with the Ubuntu machine present, and there's a whole load of traffic going on... The next is the same graph, with the Ubuntu box removed, and there's nearly no network traffic.
Then within PuTTY connected to the box he opens iptraf...
And he points to the statistics and declares, "See, this is running multiple kilobytes a second"...
Lets recap... "This machine is crippling our network usage on this box".... "using up kilobytes a second"... If any of you don't grasp how absurd this is please stop reading now, because the guy was clearly serious. He's not that old, he's younger than me, surely he realises kilobytes a second on a modern gigbit backbone is nothing? No, he seriously wants the ubuntu box silent.
So, what's causing this tiny trickle of bandwidth... Of course, he's connected to this machine with PuTTY...
"Close this window and take me to the machine in the server room"
In we go, and from the console plugged into the machine I run iptraf... look what we see now...
Yes, its zero...
When he is running his measures and checks he's connected over the network to the machine, he's been measuring his own bandwidth administrating the machine, and apparently the kilobytes per second that took were too much for him, I feel like this guy should be thrown from the building, not be earning far more than me in the "superior" position of "network guru god" which hs holds...
So knowing the machine is idle, I wonder what causes the bandwidth, so with the iptraf still running, I call up the user and ask him to get working again... and sure enough there's little trickles of bandwidth from the Ubuntu box, no spikes, no major blocks of transmission. Hence after a few minutes I conclude the Ubuntu box though active, is only taking a tiny amount of the total bandwidth available to the machine, and so I start to look at the other machines...
And with just perfmon on the two Windows Server 2008 boxes with the user operating his end, I can see that the Windows boxes are spiking their network traffic, just with task manager you can see one box taking 20% the total available to it, and the other over 45%... This is a gigbit connection, and 20% of it is taken for maybe 30-50 seconds each minute and then goes quiet, then the 45% hog is there for maybe every 10 seconds of each minute.
They're scheduled tasks, as the users input items into their programs, the programs process and send out instructions, e-mails and update other databses. The trickle of data I/O to the ubuntu machine is just such a feed, the Ubuntu box sucks/queries data from MS SQL Server on one of the machines and squirts it into MySQL where a very old (like 8 year old) unmaintained program written in C with MySQLConnector picks it up and uses sendmail to e-mail a load of people. Such notifications take maybe 0.25 seconds and run every 5 minutes, the amount of data is small, and looking at the physical name of the Ubuntu box "e-mailer relay" I think it's safe to say its not using a lot of bandwidth and the other machines are to blame.
How I educate the "network expert"... This is my next task.
Monday, 20 January 2014
C++ Integer to std::string conversion speed
There is often a lot of discussion about the most efficient way to convert things in C++, personally I like the boost::lexical_cast, I find it gives clear and readable code; very important in the systems I write, especially for maintenance and up keep.
However, many assume it to be slow, indeed most authors on the topic turn almost immediately to C for the fastest way to convert integers to strings, and unfortunately I find the same is true, even with std::string and std::ostringstream features the old sprintf tends to be faster.
But, crippling many users of std::string is their lack of understanding of that standard library staple class, so here is my little investigation into the speed of conversion, using a mix of C and standard C++, to give you a good idea of how fast things can be, and how to use your standard class and its memory to best effect.
The first trick you will see in this is the use of the std::string as a memory buffer, you can do this by declaring your standard string, then resizing it...
std::string MyName;
MyName.resize(12);
The memory location now at &MyName[0] points to 12 empty characters for you to use just as you would a char* or &char[], useful to stop using uncontrolled char* buffers left right and center, and perhaps the least used "tip" I can give when using std::string.
So, what conversions do we have?...
class Conversions
{
public:
static const std::string IntToString(const int& p_Input);
static const std::string IntToString2(const int& p_Input);
static const std::string IntToString3(const int& p_Input);
static const char* IntToString4(const int& p_Input);
static const void TestConversions (const int& p_Cycles);
};
The first three are going to be using whatever code to always give a pre-allocated std::string, the fourth conversion is going to return a raw char*, so the programmer has to delete the result etc, or rick a memory leak.
I still want that fourth result to be a std::string however, and I'm not going to worry about the leaking memory, so though the function performs a new[] I will not perform a delete[], but simply will assign the char* to a new std::string upon return. Making the timings taken fairer.
So, lets look at the program code for each of the functions, and the timing in the test function. I'm going to use boost::posix_time for the timings here:
const std::string Conversions::IntToString(const int& p_Input)
{
std::string l_buffer;
l_buffer.resize(33);
sprintf(&l_buffer[0], "%d", p_Input);
return l_buffer;
}
Above we can see the first function, using our string resizing tip, we resize the string as a buffer to take up to 33 characters (the maximum size for a 32bit integer is 33 characters) and then we use the old fashioned sprintf from the cstdlib header. Many claim this to be the fastest, the defacto conversion, even more so than using itoa.
const std::string Conversions::IntToString2(const int& p_Input)
{
std::ostringstream l_oss;
l_oss << p_Input;
return l_oss.str();
}
Next is the standard library way of working, we create a new ostringstream and stream the integer into it, then return the std::string from the stream. I believe this to be pretty slow, my mind tells me that the creation of the stream and then the extraction of the result is going to be slow; but we shall see in a moment.
const std::string Conversions::IntToString3(const int& p_Input)
{
char l_buffer[33];
sprintf(l_buffer, "%d", p_Input);
return std::string (l_buffer);
}
Next we have another use of sprintf, however, this time we're not resizing a string natively, we're creating a character string and then casting it back as a result. I think this may be the fastest, but again we shall see.
const char* Conversions::IntToString4(const int& p_Input)
{
char* l_buffer = new char[33];
sprintf(l_buffer, "%d", p_Input);
return l_buffer;
}
Finally, very similar to the third test, this conversion creates a new character array pointer as the buffer, and then uses sprintf. This is going to leak memory if we don't delete[], but I'm ignoring that for now and just testing the speed.
Now onto the conversion test function, the basic layout is, take the start time before the conversion, call the conversion a lot of times assigning the result to a std::string locally, and then take the time after and output the difference in milliseconds...
Now, the speed of this code in C++ is going to be fast in all cases, so we need enough sample calls to get a reading... I'm settling on 30 million calls, 30000000.
const void Conversions::TestConversions (const int& p_Cycles)
{
// Test 1
std::cout << "Test 1...";
boost::posix_time::ptime l_end;
boost::posix_time::ptime l_start (boost::posix_time::second_clock::local_time());
std::string l_result;
for (int i = 0; i < p_Cycles; ++i)
{
l_result = IntToString(i);
}
l_end = boost::posix_time::second_clock::local_time();
boost::posix_time::time_duration l_diff = l_end - l_start;
std::cout << l_diff.total_milliseconds() << std::endl;
// Test 2
std::cout << "Test 2...";
l_start = boost::posix_time::second_clock::local_time();
for (int i = 0; i < p_Cycles; ++i)
{
l_result = IntToString2(i);
}
l_end = boost::posix_time::second_clock::local_time();
l_diff = l_end - l_start;
std::cout << l_diff.total_milliseconds() << std::endl;
// Test 3
std::cout << "Test 3...";
l_start = boost::posix_time::second_clock::local_time();
for (int i = 0; i < p_Cycles; ++i)
{
l_result = IntToString3(i);
}
l_end = boost::posix_time::second_clock::local_time();
l_diff = l_end - l_start;
std::cout << l_diff.total_milliseconds() << std::endl;
// Test 4
std::cout << "Test 4...";
l_start = boost::posix_time::second_clock::local_time();
for (int i = 0; i < p_Cycles; ++i)
{
l_result = IntToString4(i);
}
l_end = boost::posix_time::second_clock::local_time();
l_diff = l_end - l_start;
std::cout << l_diff.total_milliseconds() << std::endl;
}
Lets see what our output is:
Test1...6000
Test2...17000
Test3...5000
Test4...6000
Immediately we can see my hunch about using the string stream is correct, its very much slower, more than twice as slow.
Surprisingly, at least to most readers - one hopes - we can see that the string::resize and use of sprintf is very close to the other uses of sprintf.
Since sprintf should be taking a constant amount of time what we've timed in tests 1, 3 and 4 is the speed of our memory management, how quickly has the function made the result available.
Some readers maybe screaming at me to use itoa, and one could do that with a char buffer, or even a resized string, thus:
std::string buffer;
buffer.resize(33);
itoa (&buffer[0], p_Input, 10);
However, itoa is not a standard function and some compilers don't supply it, therefore you must generally always use the lowest common denominator and sprintf is just that.
There is also one last avenue, the lexical cast...
// Test 5
std::cout << "Test 5...";
l_start = boost::posix_time::second_clock::local_time();
for (int i = 0; i < p_Cycles; ++i)
{
l_result = boost::lexical_cast<int>(i);
}
l_end = boost::posix_time::second_clock::local_time();
l_diff = l_end - l_start;
std::cout << l_diff.total_milliseconds() << std::endl;
Now, this approach should take into account the "bad_lexical_cast" exception, but exception handling is slow so we're ignoring that at this juncture. And assuming we have a known data source (int i) which we have in a valid range.
Our test results how are similar for the original calls...
Test 1...6000
Test 2...17000
Test 3...5000
Test 4...5850
The new fifth test...
Test 5...1000
This is very much quicker than any of the other solutions proposed...
So, there you go...
#include <boost/lexical_cast.hpp>
#include <boost/date_time/local_time/local_time.hpp>
#include <string>
#include <iostream>
int main ()
{
// Test 5
std::cout << "Test 5...";
boost::posix_time::ptime l_start =
boost::posix_time::second_clock::local_time();
for (int i = 0; i < p_Cycles; ++i)
{
l_result = boost::lexical_cast<int>(i);
}
boost::posix_time::ptime l_end =
boost::posix_time::second_clock::local_time();
boost::posix_time::time_duration l_diff = l_end - l_start;
std::cout << l_diff.total_milliseconds() << std::endl;
return 0;
}
Use lexical cast...
And about the exception handling, be smart the speed of this code is not affected by making the function throw the exception up, or by making the whole loop handle the exception once, be smart... and stop mucking about with conversions in C, you're only fooling yourself they're faster than other options.
Labels:
Boost,
C,
C++,
c++11,
cast,
Code,
code patterns,
lexical,
ostringstream,
programming,
speed,
sprintf,
standard library,
std,
std::ostringstream,
std::string,
std::string::resize,
string,
strings
Subscribe to:
Posts (Atom)


