1
0

test.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <time.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <sys/time.h>
  5. #include "cpucycles-impl.h"
  6. static long long tod(void)
  7. {
  8. struct timeval t;
  9. gettimeofday(&t,(struct timezone *) 0);
  10. return t.tv_sec * (long long) 1000000 + t.tv_usec;
  11. }
  12. long long todstart;
  13. long long todend;
  14. long long cpustart;
  15. long long cpuend;
  16. long long cyclespersecond;
  17. long long cyclespertod;
  18. long long t[1001];
  19. int main()
  20. {
  21. int j;
  22. int i;
  23. if (!cpucycles()) {
  24. fprintf(stderr,"cpucycles() = %lld\n",cpucycles());
  25. return 100;
  26. }
  27. for (i = 0;i <= 1000;++i) t[i] = cpucycles();
  28. for (i = 0;i < 1000;++i) if (t[i] > t[i + 1]) {
  29. fprintf(stderr,"t[%d] = %lld\n",i,t[i]);
  30. fprintf(stderr,"t[%d] = %lld\n",i + 1,t[i + 1]);
  31. fprintf(stderr,"cpucycles_persecond() = %lld\n",cpucycles_persecond());
  32. return 100;
  33. }
  34. if (t[0] == t[1000]) {
  35. fprintf(stderr,"t[%d] = %lld\n",0,t[0]);
  36. fprintf(stderr,"t[%d] = %lld\n",1000,t[1000]);
  37. fprintf(stderr,"cpucycles_persecond() = %lld\n",cpucycles_persecond());
  38. return 100;
  39. }
  40. cyclespersecond = cpucycles_persecond();
  41. if (cyclespersecond <= 0) {
  42. fprintf(stderr,"cpucycles_persecond() = %lld\n",cyclespersecond);
  43. return 100;
  44. }
  45. todstart = tod();
  46. cpustart = cpucycles();
  47. for (j = 0;j < 1000;++j) for (i = 0;i <= 1000;++i) t[i] = t[i] + i + j;
  48. todend = tod();
  49. cpuend = cpucycles();
  50. todend -= todstart;
  51. cpuend -= cpustart;
  52. cyclespertod = (long long) (((double) cpuend) * 1000000.0 / (double) todend);
  53. if (cyclespertod > 10 * cyclespersecond) {
  54. fprintf(stderr,"cyclespertod = %lld, cyclespersecond = %lld\n",cyclespertod,cyclespersecond);
  55. return 100;
  56. }
  57. for (i = 0;i <= 1000;++i) t[i] = cpucycles();
  58. printf("%s",cpucycles_implementation);
  59. printf(" %lld",cyclespersecond);
  60. printf(" %lld",cyclespertod);
  61. for (i = 0;i < 64;++i) printf(" %lld",t[i + 1] - t[i]);
  62. printf("\n");
  63. return 0;
  64. }