A small chestnut about using CPU cache, using CPU Cache
I. background knowledge
The Cache Memory is a temporary Memory located between the CPU and Memory. It has much smaller capacity than the Memory, but the switching speed is much faster than the Memory. The emergence of high-speed cache mainly aims to solve the conflict between the CPU operation speed and the memory read/write speed, because the CPU operation speed is much faster than the memory read/write speed, this will take the CPU a long time to wait for the data to arrive or write the data into the memory. The data in the cache is a small part of the memory, but this small part is about to be accessed by the CPU in a short time. When the CPU calls a large amount of data, you can avoid calling the memory directly from the cache to speed up reading.
---- The above is taken from the Niang encyclopedia, lazy --"
The cache is managed by the cache row. The typical cache row size is 64 bytes, and the size of one read operation is 64 bytes. Even if you only want to read an Int64, it actually reads the last 56bytes.
Therefore, if you need to operate an Int64 array with a Length of 8, read it at a time and hit it for the next time, hi ~~
II,Chestnuts
A small chestnut is designed to accumulate a two-dimensional Int64 data:
If you read data from a one-dimensional loop, it is similar to sequential reading. In this way, you can read data into the cache for one time and hit data for the next seven times, which improves the performance.
If a two-dimensional loop is used, the cache will be read once, and the latter will not be able to hit the cache. The performance must be much lower.
Iii. Chestnut code
1 using System; 2 3 namespace PerformanceOfCacheLineTest 4 { 5 class Program 6 { 7 private const Int64 ONE_DIMENSION = 1024 * 1024; 8 private const Int64 TWO_DIMENSION = 64; 9 10 static void Main(string[] args)11 {12 Int64[][] array = new Int64[ONE_DIMENSION][];13 for (int i = 0; i < ONE_DIMENSION; i++)14 {15 array[i] = new Int64[TWO_DIMENSION];16 for (int j = 0; j < TWO_DIMENSION; j++)17 {18 array[i][j] = 0;19 }20 }21 22 Console.WriteLine("starting....");23 24 DateTime start = System.DateTime.Now;25 Int64 sum = 0;26 for (int i = 0; i < ONE_DIMENSION; i++)27 {28 for (int j = 0; j < TWO_DIMENSION; j++)29 {30 sum += array[i][j];31 }32 }33 DateTime end = System.DateTime.Now;34 Console.WriteLine("one dimension first: using " + (end - start).TotalMilliseconds);35 36 37 start = System.DateTime.Now;38 for (int j = 0; j < TWO_DIMENSION; j++)39 {40 for (int i = 0; i < ONE_DIMENSION; i++)41 {42 sum += array[i][j];43 }44 }45 end = System.DateTime.Now;46 Console.WriteLine("two dimension first: using " + (end - start).TotalMilliseconds);47 48 Console.WriteLine("...ended");49 Console.Read();50 }51 }52 }
Iv. Chestnut results
V. Conclusion
The gap is quite obvious, proving the assumption.
As a meat bird, learning new things is always refreshing, and the world outlook is different *_*... There are still many things to learn !!!