// // NX04_01.C // // Test the speed and memory overhead of Windows memory allocations. // // Copyright (C) 2007 by Darek Mihocka. All Right Reserved. // Permission is granted to redistribute this source code unmodified. // #include #include void main(int argc, char **argv) { unsigned blocks = 0; unsigned bsize = 0; unsigned total = 0; unsigned msecs = GetTickCount(); unsigned fUseVA = FALSE; if (argc == 2) sscanf(&argv[1][0], "%d", &bsize); // Cmd line can override block size if (bsize == 0) bsize = 1; // minimum block size is one byte else if (bsize & 0x80000000) { bsize = 0 - bsize; fUseVA = TRUE; } for (;;) { void *pv; if (fUseVA) pv = VirtualAlloc(NULL, bsize, MEM_COMMIT, PAGE_READWRITE); else pv = malloc(bsize); if (NULL != pv) { // memset(pv, 1, 1); // uncomment to "tickle" the block blocks++; total += bsize; } else break; } msecs = GetTickCount() - msecs; printf("%9d blocks of %7d bytes, ", blocks, bsize); printf("%7d bytes block footprint, ", 2144337920UL / blocks); printf("total allocation = %4d MB, ", (total + 512*1024) / (1024*1024)); printf("%5d ms elapsed, ", msecs); printf("%4d ns per block.\n", ((1000000000UL / blocks) * msecs) / 1000); }