Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:27:10+00:00 2026-06-12T02:27:10+00:00

I have a function as follow: int doSomething(long numLoop,long arraySize){ int * buffer; buffer

  • 0

I have a function as follow:

int doSomething(long numLoop,long arraySize){
    int * buffer;
    buffer = (int*) malloc (arraySize * sizeof(int));
    long k;
int i;

    for (i=0;i<arraySize;i++)           
    buffer[i]=2;//write to make sure memory is allocated
    //start reading from cache
    for(k=0;k<numLoop;k++){
        int i;
        int temp
        for (i=0;i<arraySize;i++)           
        temp = buffer[i];
    }
}

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.

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

int doSomething(long numLoop,int a, long arraySize){
    int * buffer;
    buffer = (int*) malloc (arraySize * sizeof(int));
    long k;
    int i;

    for (i=0;i<arraySize;i++)           
        buffer[i]=2;//write to make sure memory is allocated
    //start reading from cache
    for(k=0;k<numLoop;k++){
        int temp;
            for (i=0;i<arraySize;i++){
                long randnum = rand();//max is 32767
                randnum = (randnum <<16) | rand();
                if (randnum < 0) randnum = -randnum;
                randnum%=arraySize;
                temp = buffer[randnum];
            }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:27:11+00:00Added an answer on June 12, 2026 at 2:27 am

    You are accessing the array in sequence,

    for (i=0;i<arraySize;i++)           
        temp = buffer[i];
    

    so the part you are accessing will always be in the cache since that pattern is trivial to predict. To see a cache-effect, you must access the array in a less predictable order, for example by generating (pseudo)random indices, so that you jump between the fron and the back of the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following dilemma: ob_start('processBuffer'); function processBuffer($buffer){ $betterBuffer = SomeClass::doSomething($buffer); return $betterBuffer; } function
I have the following function void DoSomething(int start[10], int end[10]) When I call it
I have the following function: func fitrange(a, x, b int) int { if a
I have a function along the lines of the following: doSomething: function () {
I have the following code snippets $(document).mousedown(function(event) { doSomething(); } I can capture the
I have following function in my code public List<string> GetpathsById(List<long> id) { List<string> paths
I have the following code: function doSomething(str){ return str+=a; } function anotherFunction(str){ return str+=b;
Imagind I have the following in VB: function doSomething() From ou In ctxt.Users.Where(Function(p) p.UserName
So if I have the following code: function DoSomething { $site = Something $app
Suppose I have the following code: function DoSomething:Boolean; var obj : TMyObject; i :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.