C에서 밀리초까지 시간차 계산하기
difftime()
은 초까지만 계산하기 때문에 밀리초까지 계산해 주는 함수가 필요하다.
/* Written by wje_lq from LinuxQuestions.org */
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
long long
timeval_diff(struct timeval *difference,
struct timeval *end_time,
struct timeval *start_time
)
{
struct timeval temp_diff;
if(difference==NULL)
{
difference=&temp_diff;
}
difference->tv_sec =end_time->tv_sec -start_time->tv_sec ;
difference->tv_usec=end_time->tv_usec-start_time->tv_usec;
/* Using while instead of if below makes the code slightly more robust. */
while(difference->tv_usec<0)
{
difference->tv_usec+=1000000;
difference->tv_sec -=1;
}
return 1000000LL*difference->tv_sec+
difference->tv_usec;
} /* timeval_diff() */
int
main(void)
{
struct timeval earlier;
struct timeval later;
struct timeval interval;
/*
* There are two ways to call timeval_diff. The first way simply returns
* the time difference as microseconds, a single integer.
*/
if(gettimeofday(&earlier,NULL))
{
perror("first gettimeofday()");
exit(1);
}
sleep(3);
if(gettimeofday(&later,NULL))
{
perror("second gettimeofday()");
exit(1);
}
printf("difference is %lld microseconds\n",
timeval_diff(NULL,&later,&earlier)
);
/*
* The second way to call timeval_diff returns the difference broken down as
* seconds and microseconds.
*/
if(gettimeofday(&earlier,NULL))
{
perror("third gettimeofday()");
exit(1);
}
sleep(4);
if(gettimeofday(&later,NULL))
{
perror("fourth gettimeofday()");
exit(1);
}
timeval_diff(&interval,&later,&earlier);
printf("difference is %ld seconds, %ld microseconds\n",
interval.tv_sec,
interval.tv_usec
);
/*
* Well, actually there's a third way. You can actually combine the first
* two ways.
*/
if(gettimeofday(&earlier,NULL))
{
perror("fifth gettimeofday()");
exit(1);
}
sleep(5);
if(gettimeofday(&later,NULL))
{
perror("sixth gettimeofday()");
exit(1);
}
printf("difference is %lld microseconds",
timeval_diff(&interval,&later,&earlier)
);
printf(" (%ld seconds, %ld microseconds)\n",
interval.tv_sec,
interval.tv_usec
);
return 0;
} /* main() */
하지만, 윈도의 비주얼 스튜디오에는 sys/time.h
헤더가 없고 gettimeofday()
함수가 정의되어 있지 않다. 그래서 직접 만들어 써야 한다.
time.h
#pragma once
#ifndef _TIMES_H
#include "times.h"
#endif
times.h
#ifndef _TIMES_H
#define _TIMES_H
#ifdef _WIN32
#include <sys/timeb.h>
#include <sys/types.h>
#include <winsock2.h>
int gettimeofday(struct timeval* t,void* timezone);
// from linux's sys/times.h
//#include <features.h>
#define __need_clock_t
#include <time.h>
/* Structure describing CPU time used by a process and its children. */
struct tms
{
clock_t tms_utime; /* User CPU time. */
clock_t tms_stime; /* System CPU time. */
clock_t tms_cutime; /* User CPU time of dead children. */
clock_t tms_cstime; /* System CPU time of dead children. */
};
/* Store the CPU time used by this process and all its
dead children (and their dead children) in BUFFER.
Return the elapsed real time, or (clock_t) -1 for errors.
All times are in CLK_TCKths of a second. */
clock_t times (struct tms *__buffer);
typedef long long suseconds_t ;
#endif
#endif
time.cpp
#include "times.h"
int gettimeofday(struct timeval* t,void* timezone)
{ struct _timeb timebuffer;
_ftime( &timebuffer );
t->tv_sec=timebuffer.time;
t->tv_usec=1000*timebuffer.millitm;
return 0;
}
clock_t times (struct tms *__buffer) {
__buffer->tms_utime = clock();
__buffer->tms_stime = 0;
__buffer->tms_cstime = 0;
__buffer->tms_cutime = 0;
return __buffer->tms_utime;
}