How to produce the cpu cache effect in C and java?

后端 未结 5 1076
情话喂你
情话喂你 2021-02-09 02:15

In Ulrich Drepper\'s paper What every programmer should know about memory, the 3rd part: CPU Caches, he shows a graph that shows the relationship between \"working set\" size an

5条回答
  •  鱼传尺愫
    2021-02-09 03:15

    Is there something wrong with my method?

    Possibly, but without seeing your actual code that cannot be answered.

    • Your description of what your code is doing does not say whether you are reading the array once or many times.

    • The array may not be big enough ... depending on your hardware. (Don't some modern chips have a 3rd level cache of a few megabytes?)

    • In the Java case in particular you have to do lots of things the right way to implement a meaningful micro-benchmark.


    In the C case:

    • You might try adjusting the C compiler's optimization switches.

    • Since your code is accessing the array serially, the compiler might be able to order the instructions so that the CPU can keep up, or the CPU might be optimistically prefetching or doing wide fetches. You could try reading the array elements in a less predictable order.

    • It is even possible that the compiler has entirely optimized the loop away because result of the loop calculation is not used for anything.

    (According to this Q&A - How much time does it take to fetch one word from memory?, a fetch from L2 cache is ~7 nanoseconds and a fetch from main memory is ~100 nanoseconds. But you are getting ~2 nanoseconds. Something clever has to be going on here to make it run as fast as you are observing.)

提交回复
热议问题