我写了一些代码来测量每个字节的cpu周期。我得到否定cpb
但不知道为什么......它向我显示cpb = -0.855553 cycles/byte
我的伪代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
uint64_t rdtsc(){
unsigned int lo,hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}
int main()
{
long double inputsSize = 1024;
long double counter = 1;
long double cpuCycleStart = rdtsc();
while(counter < 3s)
function(args);
long double cpuCycleEnd = rdtsc();
long double cpb = ((cpuCycleEnd - cpuCycleStart) / (counter * inputsSize));
printf("%Lf cycles/byte\n", cpb);
return 0;
}
编辑,改进的代码,结果是相同的(负面):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned long rdtsc( void )
{
unsigned long lo, hi;
asm( "rdtsc" : "=a" (lo), "=d" (hi) );
return( lo );
}
int main()
{
long double counter;
long double inputsSize = 1024;
char *buff = createInput(inputsSize);
long double cpuCycleStart = rdtsc();
countDownTime(3.0);
for(counter=1; !secondsElapsed; counter++)
function(args);
long cpuCycleEnd = rdtsc();
long double cpb = ((cpuCycleEnd - cpuCycleStart) / (counter * inputsSize));
printf("%Lf cycles/byte\n", cpb);
return 0;
}
真的很奇怪。写了测试代码:
printf("\n%lu cpuCycleEnd \n%lu cpuCycleStart \n", cpuCycleEnd, cpuCycleStart);
printf("\n%lu counter\n%lu inputsSize \n\n", counter, inputsSize);
long double cpb = (((long double)cpuCycleEnd - (long double)cpuCycleStart) / ((long double)counter * (long double)inputsSize));
printf("%Lf cycles/byte\n", cpb);
显示:
30534991 cpuCycleEnd
1139165971 cpuCycleStart
1273029 counter
1024 inputsSize
-0.850450 cycles/byte
任何想法?
答案 0 :(得分:4)
您正在为unsigned long
为32位的目标进行编译。
您应#include <stdint.h>
并使用uint64_t
代替unsigned long
。此外,您可能希望编译unsigned long
为64位的目标。
(注意:打印uint64_t
,#include <inttypes.h>
并使用printf("%" PRIu64 "\n", value);
。)