我有一个循环中的应用程序并使用变量。基本上它只是将一个字符串复制到变量处理它然后移动到下一个字符串。我想知道如何声明我需要使用的变量,所以我编写了这段代码来测试哪一个会更快。有趣的是,malloc比我在本地声明的变量更快。我也抛出了calloc,它的速度很慢,因为它可能会将内存归零。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/resource.h>
struct rusage ruse;
#define CPU_TIME (getrusage(RUSAGE_SELF,&ruse), ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec + 1e-6 * (ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec))
void gen_random_letters(char *random_buffer, const int len)
{
int i;
static const char alphanum[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
/* initialize random seed: */
srand (time(NULL));
for ( i = 0; i < len; ++i) {
random_buffer[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
}
random_buffer[len] = 0;
}
int main()
{
clock_t tic;
clock_t toc;
int counter;
char *buffer_calloc;
char *buffer_malloc;
char buffer_local_variable[1450];
char copy_this[1450];
double time_spent;
double first, second;
int loop_max;
loop_max = 5000000;
gen_random_letters(copy_this, sizeof(copy_this));
/* Try the locally declared variable */
tic = clock();
first = CPU_TIME;
for ( counter = 0; counter <= loop_max; counter++ )
{
//memset(buffer_local_variable,0,sizeof(buffer_local_variable));
memcpy(buffer_local_variable,copy_this,sizeof(buffer_local_variable));
}
toc = clock();
second = CPU_TIME;
time_spent = (toc - tic) / CLOCKS_PER_SEC;
printf("cpu local_variable : %.2f secs\n", second - first);
printf("Elapsed local_variable: %f seconds\n\n", time_spent);
/* Try calloc */
tic = clock();
first = CPU_TIME;
for ( counter = 0; counter <= loop_max; counter++ ){
buffer_calloc = calloc(1450,sizeof(char*));
memcpy(buffer_calloc,copy_this,sizeof(buffer_calloc));
free(buffer_calloc);
}
toc = clock();
second = CPU_TIME;
time_spent = (toc - tic) / CLOCKS_PER_SEC;
printf("cpu calloc : %.2f secs\n", second - first);
printf("Elapsed calloc : %f seconds\n\n", time_spent);
/* And now malloc */
tic = clock();
first = CPU_TIME;
for ( counter = 0; counter <= loop_max; counter++ ){
buffer_malloc = malloc(1450 * sizeof(char*));
memcpy(buffer_malloc,copy_this,sizeof(buffer_malloc));
free(buffer_malloc);
}
toc = clock();
second = CPU_TIME;
time_spent = (toc - tic) / CLOCKS_PER_SEC;
printf("Cpu malloc : %.2f secs\n", second - first);
printf("Elapsed malloc : %f seconds\n", time_spent);
return 0;
}
结果:
cpu local_variable : 0.57 secs
Elapsed local_variable : 0.000000 seconds
cpu calloc : 2.08 secs
Elapsed calloc : 2.000000 seconds
Cpu malloc : 0.39 secs
Elapsed malloc : 0.000000 seconds
我期待本地声明的变量更快,因为它的内存已经分配,而不像每个循环需要调用它的malloc。我的代码是否有缺陷,这就是为什么malloc更快或者就是它的原因。
答案 0 :(得分:2)
您的代码会在calloc
和malloc
个案例中复制错误的字节数。 sizeof(buffer_malloc)
为您提供指针的大小。
对于这些情况,请尝试使用1450而不是sizeof(...)
。
我的笔记本电脑(2015 Macbook)上的结果有上述变化:
cpu local_variable : 0.16 secs
Elapsed local_variable: 0.000000 seconds
cpu calloc : 1.60 secs
Elapsed calloc : 1.000000 seconds
Cpu malloc : 0.56 secs
Elapsed malloc : 0.000000 seconds
<强>更新强>
当您真正使用1450 * sizeof(char*)
时,您还会使用malloc
分配1450 * sizeof(char)
个字节。
在修复之后,结果会更接近:
cpu local_variable : 0.16 secs
Elapsed local_variable: 0.000000 seconds
cpu calloc : 0.76 secs
Elapsed calloc : 0.000000 seconds
Cpu malloc : 0.57 secs
Elapsed malloc : 0.000000 seconds