Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <time.h>
  4. #define ARRAY_MIN (1024) /* 1/4 smallest cache */
  5. #define ARRAY_MAX (4096*4096) /* 1/4 largest cache */
  6. int x[ARRAY_MAX]; /* array going to stride through */
  7. double get_seconds() { /* routine to read time in seconds */ __time64_t ltime;
  8. _time64( &ltime );
  9. return (double) ltime;
  10. }
  11. int label(int i) {/* generate text labels */ if (i<1e3) printf("%1dB,",i);
  12. else if (i<1e6) printf("%1dK,",i/1024);
  13. else if (i<1e9) printf("%1dM,",i/1048576);
  14. else printf("%1dG,",i/1073741824);
  15. return 0;
  16. }
  17. int _tmain(int argc, _TCHAR* argv[]) {
  18. int register nextstep, i, index, stride;
  19. int csize;
  20. double steps, tsteps;
  21. double loadtime, lastsec, sec0, sec1, sec; /* timing variables */
  22. /* Initialize output */
  23. printf(" ,");
  24. for (stride=1; stride <= ARRAY_MAX/2; stride=stride*2) label(stride*sizeof(int));
  25. printf("\n");
  26. /* Main loop for each configuration */
  27. for (csize=ARRAY_MIN; csize <= ARRAY_MAX; csize=csize*2) { label(csize*sizeof(int)); /* print cache size this loop */
  28. for (stride=1; stride <= csize/2; stride=stride*2) {
  29. /* Lay out path of memory references in array */
  30. for (index=0; index < csize; index=index+stride)
  31. x[index] = index + stride; /* pointer to next */ x[index-stride] = 0; /* loop back to beginning */
  32. /* Wait for timer to roll over */
  33. lastsec = get_seconds();
  34. do sec0 = get_seconds(); while (sec0 == lastsec);
  35. /* Walk through path in array for twenty seconds */
  36. /* This gives 5% accuracy with second resolution */
  37. steps = 0.0; /* number of steps taken */
  38. nextstep = 0; /* start at beginning of path */
  39. sec0 = get_seconds(); /* start timer */
  40. do { /* repeat until collect 20 seconds */ for (i=stride;i!=0;i=i-1) { /* keep samples same */
  41. nextstep = 0;
  42. do nextstep = x[nextstep]; /* dependency */
  43. while (nextstep != 0); }
  44. steps = steps + 1.0; /* count loop iterations */
  45. sec1 = get_seconds(); /* end timer */ } while ((sec1 - sec0) < 20.0); /* collect 20 seconds */
  46. sec = sec1 - sec0;
  47. /* Repeat empty loop to loop subtract overhead */
  48. tsteps = 0.0; /* used to match no. while iterations */
  49. sec0 = get_seconds(); /* start timer */
  50. do { /* repeat until same no. iterations as above */ for (i=stride;i!=0;i=i-1) { /* keep samples same */ index = 0;
  51. do index = index + stride;
  52. while (index < csize);
  53. }
  54. tsteps = tsteps + 1.0;
  55. sec1 = get_seconds(); /* - overhead */ } while (tsteps<steps); /* until = no. iterations */
  56. sec = sec - (sec1 - sec0);
  57. loadtime = (sec*1e9)/(steps*csize);
  58. /* write out results in .csv format for Excel */
  59. printf("%4.1f,", (loadtime<0.1) ? 0.1 : loadtime); }; /* end of inner for loop */
  60. printf("\n");
  61. }; /* end of outer for loop */
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement