ฟังก์ชั่น QueryPerformanceCounter() ใช้จับเวลาการทำงาน เป็นฟังก์ชั่นที่มีใน Windows ส่วนใน Linux ต้องเขียนฟังก์ชั่นนี้เอง โดย Code แสดงดังข้างล่างนี้
This function basically recreates the win32 QueryPerformanceCounter() on Linux, play with this code:
[code lang=”c”]
#include <sys/time.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <assert.h>
/* Helpful conversion constants. */
static const unsigned usec_per_sec = 1000000;
static const unsigned usec_per_msec = 1000;
/* These functions are written to match the win32
signatures and behavior as closely as possible.
*/
bool QueryPerformanceFrequency(int64_t *frequency)
{
/* Sanity check. */
assert(frequency != NULL);
/* gettimeofday reports to microsecond accuracy. */
*frequency = usec_per_sec;
return true;
}
bool QueryPerformanceCounter(int64_t *performance_count)
{
struct timeval time;
/* Sanity check. */
assert(performance_count != NULL);
/* Grab the current time. */
gettimeofday(&time, NULL);
*performance_count = time.tv_usec + /* Microseconds. */
time.tv_sec * usec_per_sec; /* Seconds. */
return true;
}
[/code]
ที่มา: TIGForums