Sunday, August 8, 2010

C言語(C++)で microtime を取得する

C言語で microtime を計算する方法が分からなかったので調べてみた。

#include <sys/time.h>

static long start, end;
void getMicroTime(long &sec, long &usec ){
    struct timeval now;
    int rv = gettimeofday(&now, 0);
    if (rv != 0){
        sec = 0;
        usec = 0;
        return;
    }
    sec = now.tv_sec;
    usec = now.tv_usec;
}
double getMicroTimeDouble(){
    struct timeval now;
    int rv = gettimeofday(&now, 0);
    if (rv != 0){
        return 0;
    }
    return (double)now.tv_sec + (double)now.tv_usec / 1000000;
}

No comments:

Post a Comment