time.cpp 896 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "time.h"
  2. #if _WIN32
  3. #include <windows.h>
  4. #else
  5. #include <sys/time.h>
  6. #endif
  7. namespace bbTime{
  8. #if _WIN32
  9. long start;
  10. double freq;
  11. double now(){
  12. LARGE_INTEGER icounter;
  13. QueryPerformanceCounter( &icounter );
  14. long counter=icounter.QuadPart;
  15. if( !start ){
  16. start=counter;
  17. LARGE_INTEGER ifreq;
  18. QueryPerformanceFrequency( &ifreq );
  19. freq=double( ifreq.QuadPart );
  20. return 0;
  21. }
  22. counter-=start;
  23. return double( counter )/freq;
  24. }
  25. void sleep( double seconds ){
  26. Sleep( seconds * 1000 );
  27. }
  28. #else
  29. long start;
  30. double now(){
  31. timeval tv;
  32. gettimeofday( &tv,0 );
  33. long counter=tv.tv_sec*1000000+tv.tv_usec;
  34. if( !start ){
  35. start=counter;
  36. return 0;
  37. }
  38. counter-=start;
  39. return double( counter )/1000000.0;
  40. }
  41. void sleep( double seconds ){
  42. sleep( seconds * 1000 );
  43. // usleep( seconds * 1000000 );
  44. }
  45. #endif
  46. }