Cache优化的魔力

来源:互联网 发布:梧桐叶落天下知秋 编辑:程序博客网 时间:2024/04/30 07:05

 完成同样的功能,不一样的做法,效率相差竟然10倍之多,这就是需要精通底层的理由…… 

#include <stdio.h>#include <Windows.h>// ----------------- 一个简洁的计时宏 -----------------#define tS L_TIMER_RUN#define tE L_TIMER_END#define L_TIMER_RUN(id) \\DWORD dwStart##id = GetTickCount();#define L_TIMER_END(id) \\char szMsg##id[256];\sprintf(szMsg##id, "{%s} %6d ms\r\n", #id, (GetTickCount() - dwStart##id));\printf(szMsg##id);// ----------------------------------------------------#define ARR_SIZE (1024*2)int main(void){int i,j;static int array1[ARR_SIZE][ARR_SIZE];static int array2[ARR_SIZE][ARR_SIZE];tS(test_1);for(i=0;i<ARR_SIZE;i++){for(j=0;j<ARR_SIZE;j++){array1[j][i]=array2[j][i];}}tE(test_1);  // 耗时:937mstS(test_2);for(i=0;i<ARR_SIZE;i++){for(j=0;j<ARR_SIZE;j++){array1[i][j]=array2[i][j];}}tE(test_2);  // 耗时:31msgetchar();return 0;}