搜索此博客

2012年6月1日星期五

【转】C++ Timer Function

you can use function clock() in to measure the elapsed time since program startup, e.g. to measure elapsed time for a section of code


  1. time_t  start_time = clock();                              /*  get start time  */
  2. ........ code to measure
  3.     float time1 = (float) (clock() - start_time) / CLOCKS_PER_SEC; 
  4.     printf("time for code was %f seconds\n", time1);


the actual CPU time could well be different depending upon the number of processes running, etc....


reference:http://bytes.com/topic/c/answers/576635-there-timer-function-c

【转】C++ Tic Toc function

This needs to introduction. While drinking milk i finally figured out the holy grail of C++, possibly the most universal and useful function i have ever written in any language. It formulates itself in practically three lines of code. Put the following outside the main():



double tt_tic=0;
 
void tic(){
 tt_tic = getTickCount();
}
void toc(){
 double tt_toc = (getTickCount() - tt_tic)/(getTickFrequency());
 printf ("toc: %4.3f s\n", tt_toc);
}



seriously, if you Google for “tic toc c++” you will get people writing on for ages in which way you can do this, instead of just posting the code. So here it is.
TADAA! Matlab-style tictoc functionality! You request it in your code (duh) just by stating “tic();” and then “toc();”. Also, all my code is licenced under a Creative Commons licence. This code on the other hand I have licenced under a beerware licence (http://en.wikipedia.org/wiki/Beerware). Just so you know.


reference: http://www.timzaman.nl/?p=2246&lang=en