linux 怎么取到纳秒计算的时间


------解决方案--------------------
clock_gettime可获得纳秒级精确 

  linux rdtsc 纳秒级计时  2011-06-30 21:30:19

分类: LINUX

  1. 部分代码

  2. unsigned int ts,te;
  3. unsigned int sdx,edx;
  4. unsigned long long int clock_cycles;
  5. unsigned int max=1<<31;
  6. unsigned long long int max0=(unsigned long long int )max*2;

  7. __asm__ __volatile__(
  8. "rdtsc\n\t"
  9. :"=a"(ts),"=d"(sdx));

  10. //-------------some functions need to record time-----------//
  11. b

  12. __asm__ __volatile__(
  13. "rdtsc\n\t"
  14. :"=a"(te),"=d"(edx));


  15. printf("tick ax start =%u tick end=%u \n",ts, te);
  16. printf("tick dx start =%u tick end=%u \n",sdx, edx);
  17. clock_cycles=(unsigned long long)(edx-sdx)*max0 te-ts;
  18. //or clock_cycles=(unsigned long long)(edx-sdx)*0x0100000000l+ te-ts;
  19. printf("ticks=%llu\n",clock_cycles);

你可能感兴趣的:(linux)