Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5.  
  6. int main()
  7. {
  8. clock_t begin, end;
  9.  
  10. while(1)
  11. {
  12. begin = clock();
  13. sleep(1); // delay for 1 sec
  14. end = clock();
  15. printf("Execution time = %fn",((float)(end-begin)/CLOCKS_PER_SEC));
  16.  
  17. }
  18.  
  19. }
  20.  
  21. #include <stdio.h>
  22. #include <time.h>
  23.  
  24. int main() {
  25.  
  26. time_t begin;
  27. time(&begin);
  28.  
  29. // Somethings
  30.  
  31. time_t end;
  32. time(&end);
  33.  
  34. printf("Execution time %fn", difftime(end, begin));
  35. return (0);
  36. }
  37.  
  38. #include <stdio.h>
  39. #include <time.h>
  40. #include <sys/time.h>
  41.  
  42. int main() {
  43.  
  44. struct timeval tv;
  45. gettimeofday(&tv, NULL);
  46.  
  47. double begin =
  48. (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
  49.  
  50.  
  51. sleep(2);
  52.  
  53. gettimeofday(&tv, NULL);
  54.  
  55. double end =
  56. (tv.tv_sec) * 1000 + (tv.tv_usec) / 1000 ;
  57.  
  58. printf("Execution time %fn", end - begin);
  59. return (0);
  60. }
  61.  
  62. #include <stdio.h>
  63. #include <sys/time.h>
  64. #include <unistd.h>
  65. #define ELPS_TIME(func_name) do {
  66. struct timeval tval_before, tval_after, tval_result;
  67. gettimeofday(&tval_before, NULL);
  68. func_name;
  69. gettimeofday(&tval_after, NULL);
  70. timersub(&tval_after, &tval_before, &tval_result);
  71. printf("Time elapsed: %ld.%06ld secondsn",
  72. (long int)tval_result.tv_sec,
  73. (long int)tval_result.tv_usec); } while(0)
  74.  
  75. static void test_func1() {
  76. printf("%s:", __FUNCTION__);
  77. sleep(1);
  78. }
  79.  
  80. static void test_func2() {
  81. printf("%s:", __FUNCTION__);
  82. sleep(2);
  83. }
  84.  
  85. int main() {
  86. ELPS_TIME(test_func1()); //calling test_func1
  87. ELPS_TIME(test_func2()); //calling test_func2
  88. return 0;
  89. }
  90.  
  91. test_func1:Time elapsed: 1.000103 seconds
  92. test_func2:Time elapsed: 2.000974 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement