對比執行時間實驗ios
機器規格: CPU型號:Intel Core i7-366U;windows
CPU主頻:2.0 GHz(最大睿頻:3.2GHz);緩存
核心數: 雙核心, 線程數:四線程;spa
RAM :8GB;操作系統
操做系統位數:64位。線程
在VS中編寫代碼以下:code
#include "stdafx.h" #include "iostream" #include "windows.h" #include "time.h" using namespace std; int src[2048][2048]; int dst[2048][2048]; SYSTEMTIME lpsystime; void copyij(int src[2048][2048], int dst[2048][2048]); void copyji(int src[2048][2048], int dst[2048][2048]); void printTime(); int _tmain(int argc, _TCHAR* argv[]) { printTime(); copyij(src,dst); printTime(); copyji(src, dst); printTime(); return 0; } void copyij(int src[2048][2048], int dst[2048][2048]){ int i, j; for (i = 0; i < 2048; i++) for (j = 0; j < 2048; j++) dst[i][j] = src[i][j]; } void copyji(int src[2048][2048], int dst[2048][2048]){ int i, j; for (j = 0; j < 2048; j++) for (i = 0; i < 2048; i++) dst[i][j] = src[i][j]; } void printTime(){ GetLocalTime(&lpsystime); printf("%u:%u:%u:%u\n", lpsystime.wHour, lpsystime.wMinute, lpsystime.wSecond, lpsystime.wMilliseconds); }
運行結果以下:blog
分析:io
由結果咱們能夠計算出:先運行i後運行j的時間爲16毫秒,先運行j後運行i的時間爲203毫秒相差超過12倍。這個緣由主要是由於先運行i後運行j時每一行i都被都進了告訴緩存中,讀取能夠快速處理,而若是先運行j再運行i,則須要每處理一項向緩存中存儲一次,這要就大大增長了程序的處理時間,進而產生了上面的結果。class