Can some explain the performance behavior of the following memory allocating C program?

前端 未结 5 731
半阙折子戏
半阙折子戏 2021-02-01 08:36

On my machine Time A and Time B swap depending on whether A is defined or not (which changes the order in which the two callocs are called).

I

5条回答
  •  半阙折子戏
    2021-02-01 09:01

    You should also test using malloc instead of calloc. One thing that calloc does is to fill the allocated memory with zeros.

    I believe in your case that when you calloc arr1 last and then assign to it, it is already faulted into cache memory, since it was the last one allocated and zero-filled. When you calloc arr1 first and arr2 second, then the zero-fill of arr2 pushes arr1 out of cache.

提交回复
热议问题