Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. clock_t begin, end;
  2. double time_spent;
  3.  
  4. begin = clock();
  5. /* here, do your time-consuming job */
  6. end = clock();
  7. time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  8.  
  9. $ time ./a.out
  10.  
  11. #include <time.h>
  12.  
  13. int main()
  14. {
  15. clock_t tic = clock();
  16.  
  17. my_expensive_function_which_can_spawn_threads();
  18.  
  19. clock_t toc = clock();
  20.  
  21. printf("Elapsed: %f secondsn", (double)(toc - tic) / CLOCKS_PER_SEC);
  22.  
  23. return 0;
  24. }
  25.  
  26. #include <sys/time.h>
  27.  
  28. struct timeval tv1, tv2;
  29. gettimeofday(&tv1, NULL);
  30. /* stuff to do! */
  31. gettimeofday(&tv2, NULL);
  32.  
  33. printf ("Total time = %f secondsn",
  34. (double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
  35. (double) (tv2.tv_sec - tv1.tv_sec));
  36.  
  37. /* ISO/IEC 9899:1990 7.12.1: <time.h>
  38. The macro `CLOCKS_PER_SEC' is the number per second of the value
  39. returned by the `clock' function. */
  40. /* CAE XSH, Issue 4, Version 2: <time.h>
  41. The value of CLOCKS_PER_SEC is required to be 1 million on all
  42. XSI-conformant systems. */
  43. # define CLOCKS_PER_SEC 1000000l
  44.  
  45. # if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
  46. /* Even though CLOCKS_PER_SEC has such a strange value CLK_TCK
  47. presents the real value for clock ticks per second for the system. */
  48. # include <bits/types.h>
  49. extern long int __sysconf (int);
  50. # define CLK_TCK ((__clock_t) __sysconf (2)) /* 2 is _SC_CLK_TCK */
  51. # endif
  52.  
  53. #include <time.h>
  54.  
  55. #define CPU_TIME (getrusage(RUSAGE_SELF,&ruse), ruse.ru_utime.tv_sec +
  56. ruse.ru_stime.tv_sec + 1e-6 *
  57. (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec))
  58.  
  59. int main(void) {
  60. time_t start, end;
  61. double first, second;
  62.  
  63. // Save user and CPU start time
  64. time(&start);
  65. first = CPU_TIME;
  66.  
  67. // Perform operations
  68. ...
  69.  
  70. // Save end time
  71. time(&end);
  72. second = CPU_TIME;
  73.  
  74. printf("cpu : %.2f secsn", second - first);
  75. printf("user : %d secsn", (int)(end - start));
  76. }
  77.  
  78. #include <time.h>
  79. #include <stdio.h>
  80.  
  81. int main(){
  82. clock_t start = clock();
  83. // Execuatable code
  84. clock_t stop = clock();
  85. double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
  86. printf("Time elapsed in ms: %f", elapsed);
  87. }
  88.  
  89. double difftime(time_t time1, time_t time0);
  90.  
  91. time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement