Advertisement
wai0004

benchmark.c

Sep 17th, 2013
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.31 KB | None | 0 0
  1. /*
  2. fibonacci C benchmark program v1.3
  3. made by James Wait. forgive my terrible coding, it's messy and inefficient as this is one of my first C programs
  4. Compile with "gcc -o benchmark benchmark.c"
  5. calculates 40th fibonacci number 8 times, records each calculation time and multiplies it by 8.
  6. It then averages these numbers and returns the result as the final score.
  7.  
  8. There are no command line arguments.... yet
  9.  
  10. Also... don't use this as a serious benchmarking tool. it's inacurate and bad in many other ways
  11. but it gives you a rough indication of how fast your computer is compared to other systems.
  12. ============================
  13. EXAMPLE OUTPUT/RESULTS:
  14. (RASPBERRY PI B 256MB)
  15. Result of fib(40):
  16. CALCULATING...
  17. 165580141
  18.  
  19. Benchmarking....
  20. 1/8...
  21. 2/8...
  22. 3/8...
  23. 4/8...
  24. 5/8...
  25. 6/8...
  26. 7/8...
  27. 8/8...
  28. =-=-=-=-=-=-=-=-=-=
  29. CALCULATING SCORES....
  30. 80.623535
  31. 82.071993
  32. 81.830015
  33. 80.193880
  34. 80.741247
  35. 84.901833
  36. 81.720455
  37. =-=-=-=-=-=-=-=-=-=
  38. CALCULATING AVERAGE....
  39. =-=-=-=-=-=-=-=-=-=
  40.  
  41.  
  42. =-=-=-=-=-=-=-=-=-=
  43. Final Score (lower is better)
  44. 81.726137
  45. =-=-=-=-=-=-=-=-=-=
  46.  
  47. (CUSTOM PC UBUNTU I3-2100 4GB RAM)
  48. james@james-H67MA-D2H-B3:~$ ./benchmark
  49. CPU Model:
  50. Intel(R) Core(TM) i3-2100 CPU @ 3.10GHz
  51.  
  52. Result of fib(40):
  53. Calculating....
  54. 165580141
  55.  
  56. Benchmarking....
  57. 1/8...
  58. 2/8...
  59. 3/8...
  60. 4/8...
  61. 5/8...
  62. 6/8...
  63. 7/8...
  64. 8/8...
  65. =-=-=-=-=-=-=-=-=-=
  66. Calculating Scores....
  67. 6.576176
  68. 6.553473
  69. 6.538544
  70. 6.538626
  71. 6.532127
  72. 6.535168
  73. 6.531120
  74.  
  75. Calculating Average....
  76.  
  77. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  78. Final Score (lower is better)
  79. 6.543605
  80. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  81.  
  82. (BELLARD.ORG/JSLINUX/)
  83. 4953.692964
  84. */
  85.  
  86. //START PROGRAM
  87.  
  88.  
  89. #include <stdio.h>
  90. // sys/time.h and /sys/resource.h may not work on windows
  91. #include <sys/time.h>
  92. #include <sys/resource.h>
  93. //Timer setup
  94.  
  95. //get_time MAY not work for windows.
  96. double get_time()
  97. {
  98.     struct timeval t;
  99.     struct timezone tzp;
  100.     gettimeofday(&t, &tzp);
  101.     return t.tv_sec + t.tv_usec*1e-6;
  102. }
  103. //fibonacci number function
  104. int fib(n)
  105. {
  106.     if (n < 2)
  107.         return 1;
  108.     return fib(n-2) + fib(n-1);
  109. }
  110.  
  111. //MAIN
  112. int main(int argc, char const* argv[])
  113. {
  114. //commment this section out if you are not compiling for linux
  115. //============================================================
  116.     printf("CPU Model:\n");
  117.     //Get CPU model
  118.     int n;
  119.     char line[1024], *p;
  120.     FILE *f;
  121.  
  122.     if (!(f = fopen("/proc/cpuinfo", "r"))) //reads from /proc/cpuinfo and finds the "model_name" line
  123.         err(1, "fopen");
  124.     n = 5;
  125.     while (n--)
  126.         if(!fgets(line, sizeof(line), f))
  127.             err(1, "fgets");
  128.     fclose(f);
  129.     n = strlen(line);
  130.     line[--n] = '\0';
  131.  
  132.     if ((p = strchr(line, ':')) && (p + 2 - line) < n)
  133.         fputs(p + 2, stdout);
  134.  
  135.     //finish get cpu model
  136. //==============================================================
  137.     printf("\n\nResult of fib(40):\nCalculating....\n");
  138.     //REMOVED BECAUSE CAUSED INACCURACIES
  139.     //double time = get_time(); //gets the time
  140.     printf("%d\n\n", fib(40));      //calculates and prints fib(40)
  141.     //double time2 = get_time();    //gets time took to calculate
  142.     //END
  143.     printf("Benchmarking....\n");
  144.     printf("1/8...\n");
  145.     double time3 = get_time();
  146.     fib(40);
  147.     double time4 = get_time();
  148.     printf("2/8...\n");
  149.     double time5 = get_time();
  150.     fib(40);
  151.     double time6 = get_time();
  152.     printf("3/8...\n");
  153.     double time7 = get_time();
  154.     fib(40);
  155.     double time8 = get_time();
  156.     printf("4/8...\n");
  157.     double time9 = get_time();
  158.     fib(40);
  159.     double time10 = get_time();
  160.     printf("5/8...\n");
  161.     double time11 = get_time();
  162.     fib(40);
  163.     double time12 = get_time();
  164.     printf("6/8...\n");
  165.     double time13 = get_time();
  166.     fib(40);
  167.     double time14 = get_time();
  168.     printf("7/8...\n");
  169.     double time15 = get_time();
  170.     fib(40);
  171.     double time16 = get_time();
  172.     printf("8/8...\n");
  173.     double time17 = get_time();
  174.     fib(40);
  175.     double time18 = get_time();
  176.  
  177. //
  178. //Calculates results (time & averages)
  179. //
  180. //   printf("Score (lower is better):\n");
  181.     printf("=-=-=-=-=-=-=-=-=-=\n");
  182.     printf("Calculating Scores....\n");
  183. //
  184.     // printf("%f\n", time2*8 - time*8); //prints
  185.     //double score1 = time2*8 - time*8; //gets time taken to calculate * 2
  186. //
  187.     printf("%f\n", time4*8 - time3*8);
  188.     double score2 = time4*8 - time3*8;
  189. //
  190.     printf("%f\n", time6*8 - time5*8);
  191.     double score3 = time6*8 - time5*8;
  192. //
  193.     printf("%f\n", time8*8 - time7*8);
  194.     double score4 = time8*8 - time7*8;
  195. //
  196.     printf("%f\n", time10*8 - time9*8);
  197.     double score5 = time10*8 - time9*8;
  198. //
  199.     printf("%f\n", time12*8 - time11*8);
  200.     double score6 = time12*8 - time11*8;
  201. //
  202.     printf("%f\n", time14*8 - time13*8);
  203.     double score7 = time14*8 - time13*8;
  204. //
  205.     printf("%f\n", time16*8 - time15*8);
  206.     double score8 = time16*8 - time15*8;
  207. //
  208.     printf("\n");
  209.     printf("Calculating Average....\n");
  210.     double average = score2 + score3 + score4 + score5 + score6 + score7 + score8; //calculates MEAN
  211.     double final = average / 7;             //continued
  212.     printf("\n");
  213.     printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n");
  214.     printf("Final Score (lower is better)\n");
  215.  
  216.     printf("%f\n", final);                  //prints final result
  217.     printf("=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n");
  218.  
  219.  
  220.  
  221. //printf("%d\n", get_time());
  222.     return 0;
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement