Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. /****
  2.  *
  3.  * modified version from Preston L. Bannister
  4.  *
  5.  **/
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <string.h>
  11.  
  12.  
  13. /*******************************************************************************
  14.  *******************************************************************************
  15.  * configs
  16.  *******************************************************************************
  17.  ******************************************************************************/
  18. #define LOOPS 4000000
  19.  
  20.  
  21.  
  22.  
  23.  
  24. /*******************************************************************************
  25.  *******************************************************************************
  26.  * engine
  27.  *******************************************************************************
  28.  ******************************************************************************/
  29. unsigned int dtLoop = 0;
  30. unsigned int nTotal = 0;
  31. typedef void (*doit)();
  32.  
  33. void report_times(const char* s, unsigned int dt, int bytes, int scale)
  34. {
  35.     double ts = (double)dt / CLOCKS_PER_SEC;
  36.     double mb = (double)(bytes)*LOOPS*scale / 1024/1024 ;
  37.     double rate = mb / ts;
  38.     printf("%s:\t\t %0.4f seconds (%0.1f MB in %u clocks)\n", s, ts, mb, dt);
  39. }
  40.  
  41. int time_function(doit fn, int bytes)
  42. {
  43.     clock_t t0 = clock();
  44.     int i=0;
  45.     for (i; i<LOOPS*bytes; ++i) {
  46.         (*fn)();
  47.     }
  48.     return (int)(clock() - t0);
  49. }
  50. /*******************************************************************************
  51.  * end engine
  52.  ******************************************************************************/
  53.  
  54.  
  55.  
  56.  
  57.  
  58. /*******************************************************************************
  59.  *******************************************************************************
  60.  * benchmark
  61.  *******************************************************************************
  62.  ******************************************************************************/
  63. void do_2_bytes()
  64. {
  65.     malloc(2);
  66. }
  67. void do_4_bytes()
  68. {
  69.     malloc(4);
  70. }
  71. void do_16_bytes()
  72. {
  73.     malloc(16);
  74. }
  75. void do_32_bytes()
  76. {
  77.     malloc(32);
  78. }
  79. void do_128_bytes()
  80. {
  81.     malloc(128);
  82. }
  83. void do_256_bytes()
  84. {
  85.     malloc(256);
  86. }
  87.  
  88. /*******************************************************************************
  89.  * end benchmark
  90.  ******************************************************************************/
  91.  
  92.  
  93.  
  94.  
  95.  
  96. /*******************************************************************************
  97.  *******************************************************************************
  98.  * main programm
  99.  *******************************************************************************
  100.  ******************************************************************************/
  101. int main(int ac,char** av)
  102. {
  103.     int i = atoi(av[1]);
  104.     if(i==2)
  105.         report_times("alloc 2 bytes", time_function(do_2_bytes, 20), 2, 20);
  106.     if(i==4)
  107.         report_times("alloc 4 bytes", time_function(do_4_bytes, 10), 4, 10);
  108.     if(i==16)
  109.         report_times("alloc 16 bytes", time_function(do_16_bytes, 2), 16, 2);
  110.     if(i==32)
  111.         report_times("alloc 32 bytes", time_function(do_32_bytes, 1), 32, 1);
  112.     if(i==128)
  113.         report_times("alloc 128 bytes", time_function(do_128_bytes, 1), 128, 1);
  114.  
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement