#include #include #include #include #include #include #include double get_time() { struct timeval t; gettimeofday(&t, NULL); return t.tv_sec + t.tv_usec*1e-6; } double START, T; int COUNT, COUNTER; #define start(name, count) \ printf("%s: %d it", name, count); \ fflush(stdout); \ START = get_time(); \ COUNT = count; \ COUNTER = count; \ while (COUNTER--) { #define end() \ } \ T = get_time() - START; \ printf(" / %f sec = %f it/s\n", T, COUNT / T); GSList *free_blocks = NULL; int block_size = 64 * 1024; void *list_alloc() { void *res; if (free_blocks) { res = free_blocks->data; free_blocks = g_slist_delete_link(free_blocks, free_blocks); return res; } posix_memalign(&res, 4096, 64 * 1024); return res; } void list_free(void *ptr) { free_blocks = g_slist_prepend(free_blocks, ptr); } void list_finalize() { while (free_blocks) { void *p = free_blocks->data; free_blocks = g_slist_delete_link(free_blocks, free_blocks); free(p); } } int main(int argc, char *argv[]) { int count = atoi(argv[1]); int loc[64 * 1024 / 4]; int loc2[64 * 1024 / 4]; int *p; start("nothing", count) loc[0] = 1 + loc[1]; end() start("memcpy", count / 100) loc[0] = 1 + loc[1]; memcpy(loc, loc2, sizeof(loc) / 2); end() start("g_malloc", count) p = g_malloc(64 * 1024); p[0] = 1 + p[1]; g_free(p); end() start("g_slice_alloc", count) p = g_slice_alloc(64 * 1024); p[0] = 1 + p[1]; g_slice_free1(64 * 1024, p); end() start("list_alloc", count) p = list_alloc(); p[0] = 1 + p[1]; list_free(p); end() list_finalize(); start("posix_memalign", count / 10) posix_memalign(&p, 4096, 64 * 1024); p[0] = 1 + p[1]; free(p); end() start("aligned_alloc", count / 10) p = aligned_alloc(4096, 64 * 1024); p[0] = 1 + p[1]; free(p); end() return 0; }