ご迷惑をおかけして申し訳ございません。私は次のプログラムを書いています(Linuxでは、あなたのシステムに移植できることを願っています)。
// public domain code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
const unsigned possible_word_sizes[] = {
1, 2, 3, 4, 5,
8, 12, 16, 24,
32, 48, 64, 128,
256, 384, 2048
};
long long totalsize;
// return a calloc-ed array of nbchunks malloced zones of
// somehow random size
void **
malloc_chunks (int nbchunks)
{
const int nbsizes =
(int) (sizeof (possible_word_sizes)
/sizeof (possible_word_sizes[0]));
void **ad = calloc (nbchunks, sizeof (void *));
if (!ad)
{
perror ("calloc chunks");
exit (EXIT_FAILURE);
};
for (int ix = 0; ix < nbchunks; ix++)
{
unsigned sizindex = random() % nbsizes;
unsigned size = possible_word_sizes[sizindex];
void *zon = malloc (size * sizeof (void *));
if (!zon)
{
fprintf (stderr,
"malloc#%d (%d words) failed (total %lld) %s\n",
ix, size, totalsize, strerror (errno));
exit (EXIT_FAILURE);
}
((int *) zon)[0] = ix;
totalsize += size;
ad[ix] = zon;
}
return ad;
}
void
free_chunks (void **chks, int nbchunks)
{
// first, free the two thirds of chunks in random order
for (int i = 0; 3 * i < 2 * nbchunks; i++)
{
int pix = random() % nbchunks;
if (chks[pix])
{
free (chks[pix]);
chks[pix] = NULL;
}
}
// then, free the rest in reverse order
for (int i = nbchunks - 1; i >= 0; i--)
if (chks[i])
{
free (chks[i]);
chks[i] = NULL;
}
}
int
main (int argc, char **argv)
{
assert (sizeof (int) <= sizeof (void *));
int nbchunks = (argc > 1) ? atoi (argv[1]) : 32768;
if (nbchunks < 128)
nbchunks = 128;
srandom (time (NULL));
printf ("nbchunks=%d\n", nbchunks);
void **chks = malloc_chunks (nbchunks);
clock_t clomall = clock();
printf ("clomall=%ld totalsize=%lld words\n",
(long) clomall, totalsize);
free_chunks (chks, nbchunks);
clock_t clofree = clock();
printf ("clofree=%ld\n", (long) clofree);
return 0;
}
私のDebian /シド/ x86-64でのgcc -O2 -Wall mf.c -o mf
(i3770k、16ギガ)とそれをコンパイル。私はtime ./mf 100000
を実行しました:私のシステムclock
上
nbchunks=100000
clomall=54162 totalsize=19115681 words
clofree=83895
./mf 100000 0.02s user 0.06s system 95% cpu 0.089 total
はCPUのマイクロ秒を与えます。 random
への呼び出しが無視できる場合(およびそれがわからない場合)w.r.t. malloc
& free
時間、私はあなたの観察に同意しがちです。 free
はmalloc
の2倍の速さです。私のgcc
は6.1、私のlibc
はGlibc 2.22です。
上記のベンチマークをシステムにコンパイルし、タイミングを報告するのに時間をかけてください。
FWIW、私はJerry's codeと
g++ -O3 -march=native jerry.cc -o jerry
time ./jerry; time ./jerry; time ./jerry
が
alloc time: 1940516
del time: 602203
./jerry 0.00s user 0.01s system 68% cpu 0.016 total
alloc time: 1893057
del time: 558399
./jerry 0.00s user 0.01s system 68% cpu 0.014 total
alloc time: 1818884
del time: 527618
./jerry 0.00s user 0.01s system 70% cpu 0.014 total
あなたの実際のコードを投稿してください。 – o11c
割り当て/割り当て解除はシステムコールで、 'syscall()'で呼び出せるカーネル実装を持っています。C++のnew/deleteやC++のmallocのように、libsで宣言されたすべての割り当て/解放関数は、システムコールを拡張したり、単にそれを呼び出すことができます。割り当てのカーネル実装をそのルートに書き換えることはできないため、パフォーマンスのOSによって異なります。 –
皇室前提とは何ですか? – juanchopanza