You are here:

C++/Calculate function Running time

Advertisement


Question
I have to make efiiciency check for diffrent traversal like
preorder,inorder and postorder.

I have make a switch case for user selection and difffrent
proceducers for the traversal and these procedurers are
recursive.

My Problem is that when i calculate time in the procedure
they calculate and print in vrious time i.e how many time it
does recurstion. And i calculate it in switch case it gives
start time and end time same can u help me out to solve this
??

Answer
I cannot answer this for sure as you fail to mention what you are using to obtain the times, nor the code in case you made a silly mistake. Note also that timing function execution times is a trickier business than you would think.

First off you need to consider the characteristics of the start and end times you are obtaining:

- what is the returned times' resolution?

If the timed code takes less than the resolution of the time values to execute then the difference between end and start times will be zero.

For example the C/C++ library time function returns an unsigned integer value that is typically the number of seconds elapsed since some starting point called the epoch. A modern computer can execute a heck of a lot of code in one second. The C/C++ library clock function returns a value with a resolution of CLOCKS_PER_TICK - which may have values of say 1000 (millisecond resolution) or 1000000 (microsecond resolution) or some other value, depending on the library implementation.

Anther point here is that just because you have some supposed resolution, what is the effective useful resolution? If say the value returned by a clock library function implementation with a CLOCKS_PER_TICK value of 1000000 jumps by a minimum of 1000 each time then it effectively only has millisecond resolution and not the supposed microsecond resolution indicated by the CLOCKS_PER_TICK value.

- what does the time measure?

There are two main things a time function can measure - real elapsed time - ticks since machine or process or thread started, actual date and time - sometimes called wall clock time. Or it can measure time used by a process or thread - that is it does not count the time the process or thread is suspended for some reason - waiting for a signal or I/O or because some other thread or process was scheduled to execute. Note that different implementations of the clock C/C++ library function measure different things - for an example see the code and results at the end of an old AllExperts answer of mine at:

   http://en.allexperts.com/q/C-1040/time-milliseconds-Windows.htm

Next we have to consider the sort of time your code would take to execute. Very often single executions of functions are short, especially for small data sets, and so may not take enough time to register for the effective resolution of your timing functions. Even if the calls do register a time then unless they are quite large values they are likely to fluctuate significantly if you are measuring elapsed, wall clock style times as such things as other threads executing will be counted, Even if you are measuring thread or process running time you will get variations due to issues such as memory access times differing due to differing states of system memory caches.

To make function call times more significant repeat them - say 1000 or 1000000 or more times. If your functions' execution times depend on data set size then use a larger data set. However if you are testing various algorithms then maybe you need to use several data set sizes for each algorithm to see how they perform as data set size increases. You might like to vary the number of iterations for tests so that quick running small data set tests have a significant time and large data sets do not take too long to execute. You can of course calculate the time per iteration by dividing the duration for all iterations by the number of iterations.

Be careful if you are using optimised code that the compiler does not remove any such loops entirely - this is probably less likely if you call a function in the loop as the compiler is less likely to know of any side effects the call may perform such as writing output or updating a global variable.

To try and mitigate the effects of caching run the tests more than once and ignore the first (couple maybe?) set of figures.

Finally collect many sets of timing data over many executions and average the results.

Oh, here is a useful thread of discussion on nanosecond timing resolution:

   http://stackoverflow.com/questions/275004/c-timer-function-to-provide-time-in-na...

If you need more serious code profiling then you should look at using profiling tools such as gprof (for GCC/ G++) or those that may be provided for your compiler or operating system (and possibly processor).

   http://en.wikipedia.org/wiki/List_of_performance_analysis_tools

Hope you find this of use.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2012 About.com, a part of The New York Times Company. All rights reserved.