Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include <ctime>
  4. #include <cfloat>
  5.  
  6. double timer(int *arr, int size, int times_o, int times_z) {
  7. double result = DBL_MAX, temp_result;
  8.  
  9. union ticks {
  10. unsigned long long t64;
  11. struct s32 {
  12. long th, tl;
  13. } t32;
  14. } start, end;
  15.  
  16. //"pre-heating" the cache
  17. for (int k = 0, j = 0; j < size; j++)
  18. k = arr[k];
  19.  
  20.  
  21. for (int i = 0; i < times_z; i++) {
  22.  
  23. asm("rdtsc\n":"=a"(start.t32.th), "=d"(start.t32.tl));
  24.  
  25. for (int k = 0, j = 0; j < size * times_o; j++)
  26. k = arr[k];
  27.  
  28. asm("rdtsc\n":"=a"(end.t32.th), "=d"(end.t32.tl));
  29.  
  30. temp_result = (end.t64 - start.t64) / (double) size / times_o;
  31. if (temp_result < result)
  32. result = temp_result;
  33. }
  34.  
  35. return result;
  36. }
  37.  
  38. int *get_direct_array(int size) {
  39. int *arr = (int *) malloc(size * sizeof(int));
  40.  
  41. for (int j = 0; j < size - 1; j++)
  42. arr[j] = j + 1;
  43. arr[size - 1] = 0;
  44.  
  45. return arr;
  46. }
  47.  
  48. int *get_reversed_array(int size) {
  49. int *arr = (int *) malloc(size * sizeof(int));
  50.  
  51. for (int j = 0; j < size; j++)
  52. arr[j] = j - 1;
  53. arr[0] = size - 1;
  54.  
  55. return arr;
  56. }
  57.  
  58. int *get_random_array(int size) {
  59. int *arr = (int *) malloc(size * sizeof(int));
  60. int l, temp;
  61.  
  62. srand(time(NULL));
  63.  
  64. for (int j = 0; j < size; j++)
  65. arr[j] = j;
  66.  
  67. for (int j = size - 1; j > 0; j--) {
  68. l = rand() % j;
  69. temp = arr[l];
  70. arr[l] = arr[j];
  71. arr[j] = temp;
  72. }
  73.  
  74. return arr;
  75. }
  76.  
  77. int main() {
  78. int size, step;
  79. int *arr;
  80.  
  81. FILE * file = fopen("test.csv", "w");
  82.  
  83. const int ATTEMPTS_TO_MEASURE = 10;//10;
  84. const int ATTEMPTS_TO_PASS = 5;//5;
  85.  
  86.  
  87.  
  88. const size_t kb1_step = 1024 * 1 / sizeof(int);
  89. const size_t kb4_step = 1024 * 4 / sizeof(int);
  90. const size_t kb16_step = 1024 * 32 / sizeof(int);
  91. const size_t kb128_step = 1024 * 128 / sizeof(int);
  92. const size_t kb256_step = 1024 * 256 / sizeof(int);
  93.  
  94. const size_t kb32 = 1024 * 32 / sizeof(int);//32 KB
  95. const size_t kb256 = 1024 * 256 / sizeof(int);//256 KB
  96. const size_t kb8192 = 1024 * 8192 / sizeof(int);//8192 KB
  97.  
  98. size = kb4_step;
  99. while (size <= kb8192){
  100.  
  101. if (kb32*4<=size && size<=kb256){
  102. size+= kb4_step*2;
  103. }
  104. else
  105. size += size/100 + size/1000;
  106. //size += kb32/100; //8192
  107.  
  108. if (size < 1024)
  109. fprintf(file, "%lf, ", (double) size / 1024 * 4);
  110. else
  111. fprintf(file, "%d, ", size / 1024 *4);
  112. //fprintf(file,"%d, ", size*4);
  113. arr = get_direct_array(size);
  114. fprintf(file,"%lf, ", timer(arr, size, ATTEMPTS_TO_PASS, ATTEMPTS_TO_MEASURE));
  115. free(arr);
  116.  
  117. arr = get_reversed_array(size);
  118. fprintf(file, "%lf, ", timer(arr, size, ATTEMPTS_TO_PASS, ATTEMPTS_TO_MEASURE));
  119. free(arr);
  120.  
  121. arr = get_random_array(size);
  122. fprintf(file, "%lf\n", timer(arr, size, ATTEMPTS_TO_PASS, ATTEMPTS_TO_MEASURE));
  123. free(arr);
  124.  
  125. }
  126.  
  127. fclose(file);
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement