Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. // SCSLab2.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "malloc.h"
  6. #define rdtsc __asm __emit 0fh __asm __emit 031h
  7. #define cpuid __asm __emit 0fh __asm __emit 0a2h
  8.  
  9. int * generateArray(const int n){
  10. int array [100];
  11. for(int i=0; i<n; i++){
  12. array[i] = n-i;
  13. }
  14. return array;
  15. }
  16.  
  17. int * generateArrayDynamic(int n){
  18. int ** array = (int **)malloc(n*sizeof(int));
  19. for(int i=0; i<n; i++){
  20. array[i] = (int *)malloc(sizeof(int));
  21. *array[i] = n-i;
  22. }
  23. return *array;
  24. }
  25.  
  26. void sort(int array[], int n){
  27. for(int i =0; i<n; i++){
  28. for(int j = i; j<n; j++){
  29. if(array[i] > array[j]){
  30. array[i] = array[i] + array[j];
  31. array[j] = array[i] - array[j];
  32. array[i] = array[i] - array[j];
  33. }
  34. }
  35. }
  36. }
  37.  
  38. void sortDynamic(int * array, int * next){
  39. if(array == NULL || next == NULL) return;
  40. else{
  41. if(*array > *next){
  42. int *aux = array;
  43. array = next;
  44. next = aux;
  45. }
  46. sortDynamic(array, next+1);
  47. sortDynamic(array+1, array+1);
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. unsigned cycles_high1=0, cycles_low1=0, cupid_time=0;
  54. unsigned cycles_high2=0, cycles_low2=0;
  55. unsigned __int64 temp_cycles1=0, temp_cycles2=0;
  56. __int64 total_cycles=0;
  57.  
  58. int var;
  59. //compute the CPUID overhead
  60. __asm {
  61. pushad
  62. CPUID
  63. RDTSC
  64. mov cycles_high1, edx
  65. mov cycles_low1, eax
  66. popad
  67. pushad
  68. CPUID
  69. RDTSC
  70. popad
  71.  
  72. pushad
  73. CPUID
  74. RDTSC
  75. mov cycles_high1, edx
  76. mov cycles_low1, eax
  77. popad
  78. pushad
  79. CPUID
  80. RDTSC
  81. popad
  82.  
  83.  
  84. pushad
  85. CPUID
  86. RDTSC
  87. mov cycles_high1, edx
  88. mov cycles_low1, eax
  89. popad
  90. pushad
  91. CPUID
  92. RDTSC
  93. sub eax, cycles_low1
  94. mov cupid_time, eax
  95. popad
  96.  
  97. pushad
  98. CPUID
  99. RDTSC
  100. mov cycles_high1, edx
  101. mov cycles_low1, eax
  102. popad
  103. pushad
  104. CPUID
  105. RDTSC
  106. sub eax, cycles_low1
  107. mov cupid_time, eax
  108. popad
  109. }
  110.  
  111. cycles_high1=0;
  112. cycles_low1=0;
  113.  
  114. int * array;
  115. int avr_times;
  116.  
  117. //Measure the code sequence
  118. int var1, var2 = 5;
  119. __asm {
  120. pushad
  121. CPUID
  122. RDTSC
  123. mov cycles_high1, edx
  124. mov cycles_low1, eax
  125. popad
  126. }
  127.  
  128. //Section of code to be measured
  129.  
  130. /*__asm{
  131. add var2, ecx
  132. }
  133.  
  134. __asm{
  135. add edx, ecx
  136. }
  137.  
  138. __asm{
  139. mov edx, 9
  140. mul ecx
  141. }
  142.  
  143. __asm{
  144. mov edx, 94
  145. mov ecx, 84
  146. fdiv
  147. }
  148.  
  149. __asm{
  150. mov edx, 94
  151. mov ecx, 84
  152. fsub
  153. }*/
  154.  
  155. //Sorts
  156.  
  157. array=generateArray(90);
  158. sort(array, 90);
  159.  
  160. __asm {
  161. pushad
  162. CPUID
  163. RDTSC
  164. mov cycles_high2, edx
  165. mov cycles_low2, eax
  166. popad
  167. }
  168. /*temp_cycles1 = ((unsigned __int64)cycles_high1 << 32) | cycles_low1;
  169. temp_cycles2 = ((unsigned __int64)cycles_high2 << 32) | cycles_low2;
  170. total_cycles = temp_cycles2 - temp_cycles1 - cupid_time;
  171. avr_times += total_cycles;*/
  172.  
  173. /* Exact time
  174. array=generateArrayDynamic(90);
  175. sort(array, 90);
  176. */
  177.  
  178. temp_cycles1 = ((unsigned __int64)cycles_high1 << 32) | cycles_low1;
  179. temp_cycles2 = ((unsigned __int64)cycles_high2 << 32) | cycles_low2;
  180. total_cycles = temp_cycles2 - temp_cycles1 - cupid_time;
  181.  
  182. printf("total cycles: %d\n\r", total_cycles);
  183. printf("total seconds: %f\n\r", 1.0/total_cycles);
  184. getchar();
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement