Hey guys,
do you know how to check a function execution time ?
Thanks,
Dean.
Hey guys,
do you know how to check a function execution time ?
Thanks,
Dean.
By having it execute tens of thousands of times in a very tight loop while tracking start time and stop time.
I don't know if you're on Windows or one of the *nix flavours but on Windows it goes like this...
The catch is that unless you've written some horifficly complex function with all kinds of built in loops, access to hardware or other time consuming bits and pieces, execution time for a typical function is in microseconds, well below the timer resolution in most desktop computers.Code:start = GetTickCount(); // in milliseconds for(int x = 0; x < 1000000; x++) MyFunction(); elapsed = GetTickCount() - start;
I recently did a small program to compare the speeds of stack allocation and heap allocation... It took more than 10,000,000 iterations of the two functions to expose any noticeable difference in speed... It took 10,000 iterations just to get it to show up on the tick counter.
by using profiler Find Application Bottlenecks with Visual Studio Profiler
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Look up your Compiler and function profile or profiling.
Example result link: An Introduction to GCC - Using the profiler gprof
Tim S.
Sorry... not a Linux programmer... not even a Linux user.
However, I would advise against what the gang here calls "premature optimization"... That is, unless you can track a specific problem to something taking too long, you should spend your time and effort writing good and safe code. When unnecessary, optimization is usually nothing but a frustration...
Necessary optimizations may come when (for example) you have high network traffic and are getting buffer full errors. There, you might have something that takes longer to execute than it takes to fill the network buffer... now you get busy finding out what's slowing you down.
The rest of the time, just get it right... Nothing else matters.
As for "pointers, structures and everything"... when you have control over the data structure there's a simple rule "KIS... Keep It Simple". While it won't make any difference in speed, your code will become infinitely easier to maintain if you elminate such common boondoggles as nested structs and pointers to pointers wherever you can. 9 of 10 times when I see someone writing berzerker lines like ...
... the first thing I figure is that their data probably needs reorganizing.Code:day = data -> purchase -> order -> time -> day
Code should be written with a simple question in mind:
"If I come back to this in 5 years, will I still be able to follow it?"
If the answer is no... start over.