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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:15:54+00:00 2026-06-11T23:15:54+00:00

Full Re-Write/Update for clarity (and your sanity, its abit too long) … ( Old

  • 0

Full Re-Write/Update for clarity (and your sanity, its abit too long) … (Old Post)

For an assignment, I need to find the levels (L1,L2,…) and size of each cache. Given hints and what I found so far: I think the idea is to create arrays of different sizes and read them. Timing these operations:

sizes = [1k, 4k, 256K, ...]
foreach size in sizes 
    create array of `size`

    start timer
    for i = 0 to n // just keep accessing array
        arr[(i * 16) % arr.length]++ // i * 16 supposed to modify every cache line ... see link
    record/print time

UPDATED (28 Sept 6:57PM UTC+8)

See also full source

Ok now following @mah’s advice, I might have fixed the SNR ratio problem … and also found a method of timing my code (wall_clock_time from a lab example code)

However, I seem to be getting incorrect results: I am on a Intel Core i3 2100: [SPECS]

  • L1: 2 x 32K
  • L2: 2 x 256K
  • L3: 3MB

The results I got, in a graph:

lengthMod: 1KB to 512K

enter image description here

The base of the 1st peak is 32K … reasonable … the 2nd is 384K … why? I’m expecting 256?

lengthMod: 512k to 4MB

enter image description here

Then why might this range be in a mess?


I also read about prefetching or interference from other applications, so I closed as many things as possible while the script is running, it appears consistently (through multiple runs) that the data of 1MB and above is always so messy?

  • 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-11T23:15:55+00:00Added an answer on June 11, 2026 at 11:15 pm

    The time it takes to measure your time (that is, the time just to call the clock() function) is many many (many many many….) times greater than the time it takes to perform arr[(i*16)&lengthMod]++. This extremely low signal-to-noise ratio (among other likely pitfalls) makes your plan unworkable. A large part of the problem is that you’re trying to measure a single iteration of the loop; the sample code you linked is attempting to measure a full set of iterations (read the clock before starting the loop; read it again after emerging from the loop; do not use printf() inside the loop).

    If your loop is large enough you might be able to overcome the signal-to-noise ratio problem.

    As to “what element is being incremented”; arr is an address of a 1MB buffer; arr[(i * 16) & lengthMod]++; causes (i * 16) * lengthMod to generate an offset from that address; that offset is the address of the int that gets incremented. You’re performing a shift (i * 16 will turn into i << 4), a logical and, an addition, then either a read/add/write or a single increment, depending on your CPU).

    Edit:
    As described, your code suffers from a poor SNR (signal to noise ratio) due to the relative speeds of memory access (cache or no cache) and calling functions just to measure the time. To get the timings you’re currently getting, I assume you modified the code to look something like:

    int main() {
        int steps = 64 * 1024 * 1024;
        int arr[1024 * 1024];
        int lengthMod = (1024 * 1024) - 1;
        int i;
        double timeTaken;
        clock_t start;
    
        start = clock();
        for (i = 0; i < steps; i++) {
            arr[(i * 16) & lengthMod]++;
        }
        timeTaken = (double)(clock() - start)/CLOCKS_PER_SEC;
        printf("Time for %d: %.12f \n", i, timeTaken);
    }
    

    This moves the measurement outside the loop so you’re not measuring a single access (which would really be impossible) but rather you’re measuring steps accesses.

    You’re free to increase steps as needed and this will have a direct impact on your timings. Since the times you’re receiving are too close together, and in some cases even inverted (your time oscillates between sizes, which is not likely caused by cache), you might try changing the value of steps to 256 * 1024 * 1024 or even larger.

    NOTE: You can make steps as large as you can fit into a signed int (which should be large enough), since the logical and ensures that you wrap around in your buffer.

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

Sidebar

Related Questions

I need to write php code to identify common e-mail errors, like inbox full
Full disclosure: this is for an assignment, so please don't post actual code solutions!
Update: I've done a full write-up of the way I found to do this
i want to write a query with Full-Text-Search on a column with varbinary(max) type
I'm trying to write Firefox stylish css ( full screen width style ) for
I write a big application by using of NHibernate ORM. Is using of full
Full Question: From a child table linked to a row in its grandparent table
Full disclosure : This is for a homework assignment. This is driving me nuts.
FULL EDIT: I urgently need to access a Microsoft SQL Server and read compressed
UPDATE2: Tried rendering just a quad. UPDATE: The FULL code is here. Somebody can

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.