2010年2月22日

時間量測

一般在效能調整上常用到時間量測,以下示範kernel space 及 user space 的用法.

User space:
API reference : http://www.opengroup.org/onlinepubs/009695399/basedefs/time.h.html


#include <stdio.h>
#include <time.h>

int
main ()
{
  double nDiff;
  time_t t1, t2;
  struct tm *tm_ptr;

  t1 = time ((time_t *) 0);
  tm_ptr = gmtime (&t1);

  printf ("Raw time1 is %ld\n", t1);
  printf ("date: %02d/%02d/%02d\n", tm_ptr->tm_year, tm_ptr->tm_mon + 1,
      tm_ptr->tm_mday);
  printf ("time: %02d/%02d/%02d\n", tm_ptr->tm_hour, tm_ptr->tm_min,
      tm_ptr->tm_sec);

  sleep (1);
  (void) time (&t2);

  printf ("Raw time2 is %ld\n", t2);
  printf ("date: %s\n", ctime (&t2));

  nDiff = difftime (t2, t1);
  printf ("t2-t1=%f\n", nDiff);
  return 0;

}

沒有留言: