下面是我写的一段代码,希望能对楼主有所帮助:
/**
*
file_name:
time_lost.c
*
description:
*
*
version:
1.0
*
created:
3:32 2009-7-21
*
revision:
none
*
compiler:
gcc 4.3.3
*
*
author:
prankmoon@
*
company:
*/
#include <stdio.h>
#include <stdlib.h>
/* just for test */
int foo(int n)
{
int i;
int sum = 0;
for (i=0; i<n; i++)
{
sum += n;
}
return sum;
}
/* test function */
void time_lost()
{
unsigned int start_high, start_low;
unsigned int end_high, end_low;
long long time_lost, start, end;
/* here is the core :D */
asm("rdtsc \n\t");
asm("movl %%eax, %0\n\t":"=g"(start_low));
asm("movl %%edx, %0\n\t":"=g"(start_high));
printf("start_high:\t%08X start_low:\t%08X\n", start_high, start_low);
start = start_high;
start = (start << 32) | start_low;
/* invoke the target function */
foo(100);
asm("rdtsc \n\t");
asm("movl %%eax, %0\n\t":"=g"(end_low));
asm("movl %%edx, %0\n\t":"=g"(end_high));
printf("end_high:\t%08X end_low:\t%08X\n", end_high, end_low);
end = end_high;
end = (end << 32) | end_low;
/* we did it */
printf("lost time is:\t%llX\n", end - start);
return;
}
int main(int argc, char *argv[])
{
time_lost();
return 0;
}
[[it] 本帖最后由 prankmoon 于 2009-7-21 03:58 编辑 [/it]]