#include<stdio.h>
#include<stdlib.h>
#include<linux/types.h>
#define TIMES 100
#define SIZE 1024
__u64 rdtsc()
{
__u32 lo,hi;
__asm__ __volatile__
(
"rdtsc":"=a"(lo),"=d"(hi)
);
return (__u64)hi<<32|lo;
}
int myfunction()
{
int i;
char *p = NULL;
for(i = 0;i<TIMES;i++)
{
p = (char*)malloc(SIZE*sizeof(char));
if(p)
{
free(p);
}
else
{
printf("malloc failed when i = %d\n",i);
}
}
return 0;
}
int test_rdtsc()
{
__u64 begin;
__u64 end;
begin = rdtsc();
myfunction();
end = rdtsc();
printf("myfunction cost %llu CPU cycles\n",end-begin);
return 0;
}
int main()
{
test_rdtsc();
return 0;
}
#include<stdlib.h>
#include<linux/types.h>
#define TIMES 100
#define SIZE 1024
__u64 rdtsc()
{
__u32 lo,hi;
__asm__ __volatile__
(
"rdtsc":"=a"(lo),"=d"(hi)
);
return (__u64)hi<<32|lo;
}
int myfunction()
{
int i;
char *p = NULL;
for(i = 0;i<TIMES;i++)
{
p = (char*)malloc(SIZE*sizeof(char));
if(p)
{
free(p);
}
else
{
printf("malloc failed when i = %d\n",i);
}
}
return 0;
}
int test_rdtsc()
{
__u64 begin;
__u64 end;
begin = rdtsc();
myfunction();
end = rdtsc();
printf("myfunction cost %llu CPU cycles\n",end-begin);
return 0;
}
int main()
{
test_rdtsc();
return 0;
}
本文介绍了一个简单的C程序,该程序使用rdtsc指令测量特定函数执行所消耗的CPU周期,并通过反复调用malloc与free函数来测试内存分配情况。此测试有助于理解程序运行效率及内存管理。
692

被折叠的 条评论
为什么被折叠?



