HDU4920 Matrix multiplication (CPU cache對程式的影響)
Problem Description
Given two matrices A and B of size n×n, find the product of them.
bobo hates big integers. So you are only asked to find the result modulo 3.
bobo hates big integers. So you are only asked to find the result modulo 3.
Input
The input consists of several tests. For each tests:
The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).
The first line contains n (1≤n≤800). Each of the following n lines contain n integers -- the description of the matrix A. The j-th integer in the i-th line equals Aij. The next n lines describe the matrix B in similar format (0≤Aij,Bij≤109).
Output
For each tests:
Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
Print n lines. Each of them contain n integers -- the matrix A×B in similar format.
Sample Input
1
0
1
2
0 1
2 3
4 5
6 7
Sample Output
0
0 1
2 1
經典的矩陣乘法因為第三層迴圈(最內層迴圈)是對k進行迴圈,因此b[k][j]是對b逐列進行訪問。我們知道記憶體中二維陣列是以行為單位連續儲存的,逐列訪問將會每次跳1000*4(bytes)。根據cpu cache的替換策略,將會有大量的cache失效。
因此square2.cpp將j迴圈和k迴圈交換位置,這樣就保證了
c[i][j] += a[i][k] * b[k][j];
這條語句對記憶體的訪問是連續的,增加了cache的命中率,大大提升了程式執行速度。
具體見樣例:http://blog.csdn.net/a775700879/article/details/11750703
程式碼如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 810;
int a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
int n;
int main()
{
while(~scanf("%d",&n)){
int i,j,k;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
a[i][j]%=3;
c[i][j]=0;
}
}
for(i=0;i<n;i++)
for(int j=0;j<n;j++){
scanf("%d",&b[i][j]);
b[i][j]%=3;
}
for(i=0;i<n;i++)
for(k=0;k<n;k++)
for(j=0;j<n;j++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(i=0;i<n;i++){
for(j=0;j<n-1;j++)
printf("%d ",c[i][j]%3);
printf("%d\n",c[i][n-1]%3);
}
}
return 0;
}
相關文章
- 淺析CPU結構對程式的影響以及熔斷原理
- LeetCode-Sparse Matrix MultiplicationLeetCode
- table_open_cache引數對mysql效能的影響MySql
- HDU 4920 Matrix multiplication(矩陣相乘)矩陣
- oracle實驗記錄 (predicate對cpu cost的影響)Oracle
- RAC的cache fusion對資料塊訪問效率的影響
- ZOJ Problem Set - 1094 Matrix Chain MultiplicationAI
- 例題6-3 Matrix Chain Multiplication ,Uva 442AI
- 音樂對程式設計的影響程式設計
- 基於row cache object latch研究對於sga抖動的影響Object
- 查詢快取(query_cache)的影響快取
- 終端環境對go程式的影響?Go
- 分支對程式碼效能的影響和優化優化
- try catch 對程式碼執行的效能影響
- 技術人員評估英特爾CPU新漏洞對效能的影響
- onconfig中對CPU 記憶體的利用率影響的引數記憶體
- 【Oracle】-【COMMIT對索引的影響】-從trace看COMMIT對索引的影響OracleMIT索引
- 【原創】ARM平臺記憶體和cache對xenomai實時性的影響記憶體AI
- POJ 3318 Matrix Multiplication(隨機化演算法)隨機演算法
- shrink 操作對索引的影響索引
- Update操作對索引的影響索引
- Github 對程式設計師職業生涯的影響Github程式設計師
- 完美主義對新手程式設計師的影響程式設計師
- COM套間對.NET程式使用COM物件的影響物件
- unusable index對DML/QUERY的影響Index
- Arraysize 對consistent get的影響
- mysql event對主從的影響MySql
- 新增欄位對SQL的影響SQL
- 語言對思維的影響
- Forrester:隱私對廣告程式化購買的影響REST
- 入職薪水對程式設計師的影響有多大程式設計師
- 對程式設計師最具影響的書籍清單程式設計師
- gcc常用的編譯選項對程式碼的影響(轉)GC編譯
- “影響者研究”系列(一):社會網路化時代影響者對營銷創新的影響
- 絕對定位對margin外邊距的影響
- shrink 與rebuild對索引高度的影響對比Rebuild索引
- 關於CPU Cache:程式猿需要知道的那些
- 低程式碼開發對軟體開發流程的影響