Skip to content

Instantly share code, notes, and snippets.

@a3f
Last active February 28, 2016 15:11
Show Gist options
  • Select an option

  • Save a3f/d091ff8dd6ea8af7528a to your computer and use it in GitHub Desktop.

Select an option

Save a3f/d091ff8dd6ea8af7528a to your computer and use it in GitHub Desktop.
// 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