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 8630651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:06:19+00:00 2026-06-12T09:06:19+00:00

In trying to determine the cache size for a given CPU, I tried to

  • 0

In trying to determine the cache size for a given CPU, I tried to time the memory access to memory/cache like:

lengthMod = sizes[i]/sizeof(int)  - 1; // where sizes[i] is something like 1024, 2048 ... 
for (unsigned int k = 0; k < REPS; k++) {
    data[(k * 16) & lengthMod]++;
}

1, 0.52 
4, 0.52 
8, 0.52 
16, 0.52 
32, 0.52 
64, 1.11 // << note the jump in timing. L1 cache size is 32K
128, 1.12 
256, 1.19 

So I think if the lengthMod is not a power of 2, I cant do this. So I tried doing

lengthMod = sizes[i]/sizeof(int);
for (unsigned int k = 0; k < REPS; k++) {
    data[(k * 16) % lengthMod]++;
}

1, 2.67 
4, 2.57 
8, 2.55 
16, 2.51 
32, 2.42 
64, 2.42 // << no jump anymore ...
128, 2.42 
256, 2.42

Then I find that the timing increase that I expected is non-existant anymore … I expected the time to increase but it should apply to all values? So if its x seconds when using &, I’d expect ~x+c seconds (where c is approximatly constant), but thats not the case, in fact, it reduces the timing difference to non-existant why is that?

  • 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-12T09:06:20+00:00Added an answer on June 12, 2026 at 9:06 am

    What you’re seeing is a trade-off of bottlenecks.

    • In the first example, you are bottlenecked by your cache bandwidth.
    • In the second example, you are bottlenecked by integer division.

    Before we continue, let’s look at the difference between the two examples:

    • In the first case, you use & which is a fast bitwise operation.
    • In the second case, you use % which is very slow division.

    Divisions are very slow. Modern compilers will try to optimize them when the divisor/modulus is a compile-time constant.

    But that’s not the case here. So you pay the full cost of a hardware division. This is why the times in your second example are much slower than the first.


    With the &, the code is fast enough to max out the cache bandwidth. However, with %, the code is much slower – not fast enough to keep up with the cache. So you see the same times all the way up.

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

Sidebar

Related Questions

I trying to determine display vertical size of my Blackberry Storm 2. I know,
I spent a fair amount of time trying to determine why my view of
I'm trying to determine the best way to format two columns of text like
I'm trying to determine how much heap any given TYPE_INT_ARGB BufferedImage will use so
I've been trying determine the uncompressed size of a compressed database dump. I've been
I am trying to determine the best time efficient algorithm to accomplish the task
I'm trying to determine the peak (heap) memory usage of a Perl script using
Trying to determine if it is possible to bind the SelectedValue of a ComboBox
I'm trying to determine if the current system is 32 or 64 bit. And
I am trying to determine if a phone is located in this polygon using

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.