PDA

View Full Version : How to optimize C++ code in Linux?



MutantJohn
04-01-2014, 01:00 AM
Hey All,

I'm not sure if this is the right place (do I put this in Linux? Tech?) but I'm trying to profile my software to identify bottlenecks or where my code is the slowest.

I just googled around and found out about gprof. I tried using it and the results don't seem that straight forward. Are there any other good tools or tips anyone may have about how to optimize code?

Keep in mind, this is code that was first written to work so I wasn't expecting anything great but when I try to make it lean, it's not where I want it to be.

Salem
04-01-2014, 03:50 AM
I would suggest you write a simple program and then try and gprof that as well.

Eg.


void copy1 ( char *dest, const char *src ) {
for ( i = 0 ; i <= strlen(src) ; i++ ) dest[i] = src[i];
}
void copy2 ( char *dest, const char *src ) {
int len = strlen(src);
for ( i = 0 ; i <= len ; i++ ) dest[i] = src[i];
}
void copy3 ( char *dest, const char *src ) {
while ( (dest[i] = src[i]) != 0 ) i++;
}


gprof gives you a lot of info, so it takes time to learn how to use that data effectively.

What you're basically looking for are counts of function invocations which are much larger than the size of the input problem, or times which seem much larger than the amount of work being done.

Oh, and one more thing.
You'll need an effective test suite before you start messing about with the code trying to make it quicker.
After each "improvement", you need a way to be able to quickly tell whether it still WORKS, regardless of whether it is any quicker or not.

Elysia
04-01-2014, 05:27 AM
I'm not sure if this is the right place (do I put this in Linux? Tech?) but I'm trying to profile my software to identify bottlenecks or where my code is the slowest.

This forum is usually for questions concerning the language itself, not tools, compilers, OS-specific API, etc.
So to answer your question: the most appropriate place would be the Linux section.

Hodor
04-01-2014, 06:49 AM
Good tools? The best tool you have is your mind.

grumpy
04-01-2014, 10:27 AM
Good tools? The best tool you have is your mind.

True, but everyone uses that tool differently, so quality of results vary from the dreadful to the sublime.

brewbuck
04-01-2014, 11:43 AM
Good tools? The best tool you have is your mind.

He who works without tools is a fool.