일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Software Engineering
- Book review
- Italy
- programming_book
- management
- Python
- Spain
- Malaysia
- RFID
- comic agile
- leadership
- Programming
- Book
- ubuntu
- program
- web
- agile
- essay
- django
- Linux
- MySQL
- erlang
- hbase
- Kuala Lumpur
- psychology
- hadoop
- Java
- history
- France
- QT
- Today
- Total
struct timeval, 시간 측정 본문
gcc 3.4.5 (mingw), Windows XP
#include <sys/time.h> // may be different according to Platform
struct timeval calcConsumedTime(struct timeval z_stStartTime, struct timeval z_stEndTime)
{
struct timeval z_stConsumedTime;
z_stConsumedTime.tv_sec = z_stEndTime.tv_sec - z_stStartTime.tv_sec;
z_stConsumedTime.tv_usec = z_stEndTime.tv_usec - z_stStartTime.tv_usec;
if ( z_stConsumedTime.tv_usec < 0 )
{
z_stConsumedTime.tv_sec -= 1;
z_stConsumedTime.tv_usec = (z_stEndTime.tv_usec - z_stStartTime.tv_usec + 1000000);
}
return z_stConsumedTime;
} // end of function calcConsumedTime()
int main()
{
int i;
struct timeval z_stStartTime, z_stEndTime, z_stConsumedTime;
// write start time
if ( gettimeofday(&z_stStartTime, (void*)NULL) == -1 )
{
printf("time variable initialization error\n");
return -1;
}
for ( i = 0; i < 10000000; ++i )
;
// write end time
if ( gettimeofday(&z_stEndTime, (void*)NULL) == -1 )
{
printf("time variable initialization error\n");
return -1;
}
printf("function timeCheck() took %ld.%03ld seconds\n", z_stStartTime.tv_sec,
z_stStartTime.tv_usec / 1000); // pay attention to divide 'tv_usec' by 1000
printf("function timeCheck() took %ld.%03ld seconds\n", z_stEndTime.tv_sec,
z_stEndTime.tv_usec / 1000); // pay attention to divide 'tv_usec' by 1000
z_stConsumedTime = calcConsumedTime(z_stStartTime, z_stEndTime);
printf("function timeCheck() took %ld.%03ld seconds\n", z_stConsumedTime.tv_sec,
z_stConsumedTime.tv_usec / 1000); // pay attention to divide 'tv_usec' by 1000
return 0;
} // end of function main()