Note that there are some explanatory texts on larger screens.

plurals
  1. POMeasuring cache size in C
    primarykey
    data
    text
    <p>I have a function as follow:</p> <pre><code>int doSomething(long numLoop,long arraySize){ int * buffer; buffer = (int*) malloc (arraySize * sizeof(int)); long k; int i; for (i=0;i&lt;arraySize;i++) buffer[i]=2;//write to make sure memory is allocated //start reading from cache for(k=0;k&lt;numLoop;k++){ int i; int temp for (i=0;i&lt;arraySize;i++) temp = buffer[i]; } } </code></pre> <p>What it do is to declare an array and read from the beginning to the end. The purpose is to see the effect of cache. What I expect to see is: when I call doSomething(10000,1000), the arraySize is small so it is all stored in the cache. After that I call doSomething(100,100000), the arraySize is bigger than that of the cache. As a result, the 2nd function call should take longer than the 1st one. The latter function call involved in some memory access as the whole array cannot be stored in the cache. However, it seems that the 2nd operation takes approximately the same time as the 1st one. So what's wrong here? I tried to compile with -O0 and it doesnt solve the problem. Thank you.</p> <p>Update 1: these are the code with random access and it seems to work, time access with large array is ~15s while small array is ~3s</p> <pre><code>int doSomething(long numLoop,int a, long arraySize){ int * buffer; buffer = (int*) malloc (arraySize * sizeof(int)); long k; int i; for (i=0;i&lt;arraySize;i++) buffer[i]=2;//write to make sure memory is allocated //start reading from cache for(k=0;k&lt;numLoop;k++){ int temp; for (i=0;i&lt;arraySize;i++){ long randnum = rand();//max is 32767 randnum = (randnum &lt;&lt;16) | rand(); if (randnum &lt; 0) randnum = -randnum; randnum%=arraySize; temp = buffer[randnum]; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload