fork download
  1. #include <ctime>
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. int fn()
  6. {
  7. int r = 0 ;
  8. for( int i = 0 ; i < 1000 ; ++i ) r += std::rand() % 2 ;
  9. return r ;
  10. }
  11.  
  12. int main()
  13. {
  14. // get an estimate of the processor time used
  15. auto start = std::clock() ;
  16.  
  17. // operation to time
  18. static volatile int v = 0 ;
  19. constexpr int N = 1000 ;
  20. for( int i = 0 ; i < N ; ++i ) v += fn() ;
  21.  
  22. const double elapsed = std::clock() - start ;
  23. std::cout << "approximate processor time for a single call of fn: "
  24. << (elapsed*1000000) / (CLOCKS_PER_SEC*N)
  25. << " microseconds\n" ;
  26. }
  27.  
Success #stdin #stdout 0.02s 3340KB
stdin
Standard input is empty
stdout
approximate processor time for a single call of fn: 20 microseconds