Watcom C 的笔记4 Watcom C (4)
运行时间的计算
使用RDTSC指令取出两次之间CPU运行的周期数,可以用来
计算程序的效率。
#include <stdio.h>
#include <ctype.h>
unsigned __int64 rdtsc(void);
#pragma aux rdtsc =\
0x0F 0x31 \
value [edx eax] \
parm nomemory \
modify exact [edx eax] \
nomemory;
int main()
{
int i;
unsigned __int64 start = rdtsc (),avege;
printf("Start at: %Ld\n",start);
for (i=0;i<1000000;i++)
{
}
avege = rdtsc () -start;
printf("end at: %Ld\n",avege);
return( 0 );
}
用这个东西来进行一下多位数组的访问效率测试。对于多位数组,
采用的是行优先。比如: a[2][3] 存储起来就是 a[0][0] a[0][1]... ...
在访问时,使用 a[x][y] 和 a[x][y+1] 这样的形式会有优势。
#include <stdio.h>
#include <ctype.h>
unsigned __int64 rdtsc(void);
#pragma aux rdtsc =\
0x0F 0x31 \
value [edx eax] \
parm nomemory \
modify exact [edx eax] \
nomemory;
int main()
{
int i,j,k;
int a[1000][1000];
unsigned __int64 start;
start= rdtsc();
for (i=0;i<1000;i++)
{
for (j=0;j<1000;j++)
{
k=a[i][j];
}
}
printf("First method cost %Ld\n",rdtsc() - start);
start= rdtsc();
for (i=0;i<1000;i++)
{
for (j=0;j<1000;j++)
{
k=a[i][j];
}
}
printf("Second method cost %Ld\n",rdtsc() - start);
return( 0 );
}
本机运行测试结果:
DOS/4GW Protected Mode Run-time Version 1.97
Copyright (c) Rational Systems, Inc. 1990-1994
First method cost 2433405
Second method cost 1702575
参考资料:
http://developer.novell.com/wiki/index.php/How_to_use_RDTSC_with_Watcom_C_C-plus-plus_and_CodeWarrior_PDK
1月12日