Thursday, November 18, 2010

Timing in C

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>


typedef
uint64_t u64;
static inline
u64 T2L()
{

struct
timeval tv;
gettimeofday(&tv, NULL);
return
1000000ULL * tv.tv_sec + tv.tv_usec;
}


static
u64 _t_start_;
u64 tstart()
{

_t_start_ = T2L();
return
_t_start_;
}


u64 tdiff()
{

return
T2L() - _t_start_;
}


void
ptdiff()
{

u64 d = T2L() - _t_start_;
printf("%lu sec %lu usec\n", d/1000000, d%1000000);
}

2 comments:

  1. Fixed-width font and intentations would make this easier to read
    - Krystian :)

    ReplyDelete
  2. seems better now, I have use http://www.bedaux.net/cpp2html/ to process the code to a html code. blogspot does not do the job automatically for me!

    ReplyDelete