TIMING.C 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "timing.h"
  2. #ifdef TIMING
  3. #ifndef __alpha
  4. #include <stdio.h>
  5. static struct {
  6. unsigned int st_time[2];
  7. unsigned int cycles;
  8. unsigned int calls;
  9. } timingInfo[64];
  10. static int timingEnters;
  11. static void rdtsc(unsigned int t[2])
  12. {
  13. __asm
  14. {
  15. mov esi, t
  16. _emit 0xf
  17. _emit 0x31
  18. mov [esi], eax
  19. mov [esi+4], edx
  20. }
  21. }
  22. void _timingInit(void)
  23. {
  24. memset(timingInfo,0,sizeof(timingInfo));
  25. }
  26. void _timingEnter(int which)
  27. {
  28. // if (!timingEnters++) __asm cli
  29. rdtsc(timingInfo[which].st_time);
  30. }
  31. void _timingLeave(int which)
  32. {
  33. unsigned int t[2];
  34. rdtsc(t);
  35. // if (!--timingEnters) __asm sti
  36. if (t[1]==timingInfo[which].st_time[1])
  37. {
  38. timingInfo[which].cycles += t[0]-timingInfo[which].st_time[0];
  39. }
  40. else
  41. {
  42. timingInfo[which].cycles += t[0]+(0xffffffff-timingInfo[which].st_time[0]);
  43. }
  44. timingInfo[which].calls++;
  45. }
  46. void _timingPrint(void)
  47. {
  48. int x;
  49. FILE *fp = fopen("C:\\timings.txt","wt");
  50. for (x = 0; x < sizeof(timingInfo)/sizeof(timingInfo[0]); x ++)
  51. {
  52. if (timingInfo[x].calls)
  53. fprintf(fp,"%d: %d calls, %d clocks/call\n",x,timingInfo[x].calls,timingInfo[x].cycles/timingInfo[x].calls);
  54. }
  55. timingInit();
  56. fclose(fp);
  57. }
  58. #endif
  59. #endif