[英]Free function is terribly slow in CodeBlocks
所以我使用的是 64 位 ubuntu,我使用的是帶有 GNU GCC 編譯器的 CodeBlocks。 我的代碼有問題的部分基本上是這樣的
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;
}
沒有釋放所有 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;
}
測試
time malloc 0.000046
time free 0.000003
您可以在線試用該程序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.