Performance malloc vs mmap

I conducted a performance test when writing 128 million ints to memory allocated using malloc and to a mapped memory file (backed up by a file on disk) using mmap ... I expected the results to be somewhat similar, as I understand that when writing data is initially written to memory in the associated memory file, and pdflush writes to the disk in the background (with a frequency that can be configured). With malloc, recording 128M ints took 0.55 seconds; mmap took 1.9 seconds.

So my question is: why is the difference. My initials were that pdflush overflows the bus or that when pdflush accesses memory, it blocks records ... However, starting the mmap version a second time produced a result of .52 seconds (due to caching), which leads me to think each page for mmap is not allocated until it is written (despite the fact that it is reserved for calling in mmap) ... also I understand that the memory created by malloc is not actually allocated until the first write .. Maybe whether the initial difference be, because after the initial write to memory using malloc the whole block is also highlighted with mmap, every time a new page is written, os should first select it?

UPDATE:

os : CentOS Linux version 7.0.1406 (Core) Kernel : 3.10.0-123.el7.x86_64 gcc : 4.8.2

CODE:

int* pint = malloc(128000000 * sizeof(int));
int* pint_copy = pint;

clock_t start = clock();

int i;
for(i = 0; i < 128000000; ++i)
{
    *pint++ = i;
}   

clock_t end = clock();

double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("%f\n", cpu_time_used);

free(pint_copy);

against

int fd = open("db", O_RDWR | O_CREAT, 0666);
const size_t region_size = ((512000000 / sysconf(_SC_PAGE_SIZE)) + 1) * sysconf(_SC_PAGE_SIZE); 

int return_code = ftruncate(fd, region_size);

if (return_code < 0)
    printf("mapped memory file could not be truncated: %u\n", return_code);

int* pint = mmap(NULL, region_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
int* pint_copy = pint;
close(fd);  

clock_t start = clock();

int i;
for(i = 0; i < 128000000; ++i)
{
    *pint++ = i;
}   

clock_t end = clock();

double cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("%f\n", cpu_time_used);

fgetc(stdin);

munmap(pint_copy, region_size);

ADD:

int z = 512;
while(z < 128000000)
{
    pint[z] = 0;

    z += 1024;
}

BEFORE:

  clock_t start = clock();     

Produces 0.37 seconds for both tests, leading me to believe that “tapping” each page causes os to allocate physical memory (for both mmap and malloc) ... this could also be partially due to the “tapping” pages moves some of the memory for caching ... does anyone know that with a large write to the memory (for a long period of time) pdflush blocks or slows down the memory write?

+4
source share
2 answers

, . , mmap, , . , write-back ( ) - ( ).

+1

, , .

, , (malloc'd), - (mmap'd) . , , - , , , -.

+1

All Articles