Based on the comments:
- ~90 misses if
malloc()
is used. - 32 misses if
calloc()
is used or if the array is iterated through before hand.
The reason is due to lazy allocation. The OS isn't actually giving you the memory until you touch it.
When you first touch the page, it will result in a page-fault. The OS will trap this page-fault and properly allocate it on the fly (which involves zeroing among other things). This is the overhead that is resulting in all those extra TLB misses.
But if you use calloc()
or you touch all the memory ahead of time, you move this overhead to before you start the counters. Hence the smaller result.
As for the 32 remaining misses... I have no idea.
(Or as mentioned in the comments, it's probably the PAPI interference.)