Skip to content

Instantly share code, notes, and snippets.

@ivmarkp
Last active February 23, 2017 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivmarkp/d549f9af02c769d5496987bf9aef12c2 to your computer and use it in GitHub Desktop.
Save ivmarkp/d549f9af02c769d5496987bf9aef12c2 to your computer and use it in GitHub Desktop.
Test whether using arma::max function is faster than using loops.
#include <iostream>
#include <vector>
#include <ctime>
#include <armadillo>
using namespace std;
using namespace arma;
int main() {
mat A = randu<mat>(10000,10000);
mat B = randu<mat>(10000,10000);
vector<vector<int> > C(10000, vector<int>(10000, 1));
vector<vector<int> > D(10000, vector<int>(10000, 2));
clock_t t1;
t1 = clock();
A = arma::max(A, B);
t1 = clock() - t1;
double time_taken1 = ((double)t1) / CLOCKS_PER_SEC;
clock_t t2;
t2 = clock();
for (int i = 0; i < 10000; ++i)
for (int j = 0; j < 10000; ++j)
if (C[i][j] < D[i][j])
C[i][j] = D[i][j];
t2 = clock() - t2;
double time_taken2 = ((double)t2) / CLOCKS_PER_SEC;
cout << "arma::max took " << time_taken1 << " seconds" << endl;
cout << "Iterating loops took " << time_taken2 << " seconds" << endl;
if (time_taken1 < time_taken2)
cout << "arma::max is faster than loops by "
<< ((time_taken2 - time_taken1) / time_taken1) * 100 << " %" << endl;
else
cout << "Using loops is faster than arma::max by "
<< ((time_taken1 - time_taken2) / time_taken2) * 100 << " %" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment