C/C++遍歷二維數組,列優先(column-major)比行優先(row-major)慢,why?
簡單粗暴的答案:存在Cache機制!
稍微啰嗦一點:CPU訪問內存(讀/寫,遍歷數組的話主要是讀),不是每次都直接從內存上操作,而是先看Cache里是否有所指定地址的值!
這個問題,stackoverflow上有人問過的,結論是:CPU讀取內存某地址處的值,並不是每次都去內存中取出來,有時候會從cache里讀取。當初次訪問數組的時候,會把連續一塊(chunk)內存地址上的值都讀到cache里(比如,64字節),后續CPU接受到一個內存地址要讀取數據時,先看cache里有沒有,沒有的話再去內存上取。
關鍵句:The important factor is that the cache doesn't contain individual bytes or words, it holds chunks of adjacent memory, known as a cache-line, typically 64-bytes in size. So when address X is in the cache the CPU probably doesn't need to pre-emptively fetch X+1 because it has probably already got it (except in the case where X is the last byte in a cache-line, in which case it probably will have pre-fetched the next cache-line).