C 自由函数在代码块中非常慢
所以我使用ubuntu 64位,我使用GNU GCC编译器的代码块。我的代码中有问题的部分基本上是这样的C 自由函数在代码块中非常慢,c,performance,codeblocks,free,C,Performance,Codeblocks,Free,所以我使用ubuntu 64位,我使用GNU GCC编译器的代码块。我的代码中有问题的部分基本上是这样的 int testFunction() { int i; char* test[10]; for(i = 0; i < 10; i++){ test[i] = malloc(50*(sizeof(char))); } for(i = 0; i < 10; i++){ free(test[i]); } return 0;
int testFunction() {
int i;
char* test[10];
for(i = 0; i < 10; i++){
test[i] = malloc(50*(sizeof(char)));
}
for(i = 0; i < 10; i++){
free(test[i]);
}
return 0;
}
int testFunction(){
int i;
字符*测试[10];
对于(i=0;i<10;i++){
测试[i]=malloc(50*(sizeof(char));
}
对于(i=0;i<10;i++){
免费(测试[i]);
}
返回0;
}
如果不释放所有的东西,malloc的运行时间是2秒,而有空闲部分,则是20秒atm。你知道为什么吗?你可以测量你的时间来确定
#include <stdlib.h>
#include <bits/time.h>
#include <time.h>
#include <stdio.h>
int main() {
int i;
char *test[10];
clock_t begin = clock();
for (i = 0; i < 10; i++) {
test[i] = malloc(50 * (sizeof(char)));
}
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("time malloc %f\n", time_spent);
begin = clock();
for (i = 0; i < 10; i++) {
free(test[i]);
}
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("time free %f\n", time_spent);
return 0;
}
你可以试试这个程序 什么样的古老机器才能使第一个循环在2秒内运行?我想你在测量其他东西。每次程序运行时都会调用数千次。请显示a。这与IDE有什么关系??这是一个标准库/操作系统/提供函数的任何东西的问题。而且,如果没有迈克尔·瓦尔兹已经要求的答案,就不可能提供一个合格的答案。
time malloc 0.000046
time free 0.000003