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

  • SEARCH
  • Home
  • 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 7751943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:40:40+00:00 2026-06-01T11:40:40+00:00

Given the following code : for (int i=0; i<n; i++) { counter += myArray[i];

  • 0

Given the following code :

for (int i=0; i<n; i++)
{
  counter += myArray[i];
}

And the Loop unrolling version :

for (int i=0; i<n; i+=4)
{
  counter1 += myArray[i+0];
  counter2 += myArray[i+1];
  counter3 += myArray[i+2];
  counter4 += myArray[i+3];
}

total = counter1+ counter2 + counter3+ counter4;
  1. Why do we have a cache miss in the first version ?
  2. Is the second version has indeed a better performance than the 1st ? why ?

Regards

  • 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-01T11:40:41+00:00Added an answer on June 1, 2026 at 11:40 am

    Why do we have a cache miss in the first version ?

    As Oli points out in the comments. This question is unfounded. If the data is already in the cache, then there will be no cache misses.

    That aside, there is no difference in memory access between your two examples. So that will not likely be a factor in any performance difference between them.

    Is the second version has indeed a better performance than the 1st ? why ?

    Usually, the thing to do is to actually measure. But in this particular example, I’d say that it will likely be faster. Not because of better cache access, but because of the loop-unrolling.

    The optimization that you are doing is called “Node-Splitting”, where you separate the counter variable for the purpose of breaking the dependency chain.

    However, in this case, you are doing a trivial reduction operation. Many modern compilers are able to recognize this pattern and do this node-splitting for you.

    So is it faster? Most likely. But you should check to see if the compiler does it for you.


    For the record: I just tested this on Visual Studio 2010.
    And I am quite surprised that it is not able to do this optimization.

    ; 129  : 
    ; 130  :     int counter = 0;
    ; 131  : 
    ; 132  :     for (int i=0; i<n; i++)
        mov ecx, DWORD PTR n$[rsp]
        xor edx, edx
        test    ecx, ecx
        jle SHORT $LN1@main
    $LL3@main:
    
    ; 133  :     {
    ; 134  :         counter += myArray[i];
    
        add edx, DWORD PTR [rax]
        add rax, 4
        dec rcx
        jne SHORT $LL3@main
    $LN1@main:
    
    ; 135  :     }
    

    Visual Studio 2010 does not seem to be capable of performing “Node Splitting” for this (trivial) example…

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

Sidebar

Related Questions

Given the following code: int *a = NULL; a = calloc(1, sizeof(*a)); printf(%d\n, a);
Given the following code: int i; ... ostingstream os; os<<i; string s=os.str(); I want
In c#, Given the following code: public class Person { public int PersonID {
Given the following C# code: var product = new List<int>(); for (int n1 =
Given is the following code: import java.io.Serializable; import java.util.concurrent.locks.ReentrantLock; class Data { private int
given the following code: /* signatures */ int getParams(char params[MAX_PARAM_LEN][MAX_LINE_LEN]); int getVersion(const char params[MAX_PARAM_LEN][MAX_LINE_LEN],
Given the following code snippets, is there any appreciable difference? public boolean foo(int input)
Given the following code: class Screen; class WindowMgr { WindowMgr& relocateScreen( int r, int
So, given then following code type MyClass () = let items = Dictionary<string,int>() do
Given the following code: public struct Foo { public Foo(int bar, int baz) :

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.