Last active
February 28, 2016 15:11
-
-
Save a3f/d091ff8dd6ea8af7528a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // http://stackoverflow.com/questions/11413855/why-is-transposing-a-matrix-of-512x512-much-slower-than-transposing-a-matrix-of | |
| #define SAMPLES 100 | |
| #ifndef MATSIZE | |
| #define MATSIZE 512 | |
| #endif | |
| #include <ctime> | |
| #include <iostream> | |
| int mat[MATSIZE][MATSIZE]; | |
| void transpose() | |
| { | |
| for ( int i = 0 ; i < MATSIZE ; i++ ) | |
| for ( int j = 0 ; j < MATSIZE ; j++ ) | |
| { | |
| int aux = mat[i][j]; | |
| mat[i][j] = mat[j][i]; | |
| mat[j][i] = aux; | |
| } | |
| } | |
| int main() | |
| { | |
| //initialize matrix | |
| for ( int i = 0 ; i < MATSIZE ; i++ ) | |
| for ( int j = 0 ; j < MATSIZE ; j++ ) | |
| mat[i][j] = i+j; | |
| int t = std::clock(); | |
| for ( int i = 0 ; i < SAMPLES ; i++ ) | |
| transpose(); | |
| int elapsed = std::clock() - t; | |
| std::cout << "Average for a matrix of " << MATSIZE << ": " << elapsed / SAMPLES << std::endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment