Skip to content

Instantly share code, notes, and snippets.

@mtao
Last active January 14, 2019 17:54
Show Gist options
  • Save mtao/c730a69b19fa5ccaf49e4df97a0fad47 to your computer and use it in GitHub Desktop.
Save mtao/c730a69b19fa5ccaf49e4df97a0fad47 to your computer and use it in GitHub Desktop.
Had odd performance using asDiagonal at https://github.com/mtao/core/blob/master/include/mtao/eigen/mic0_preconditioner.hpp#L160 so I switched to cwiseProduct. They certainly have deferring performance, especially with and without O2. A sheet looking at the difference is here: https://docs.google.com/spreadsheets/d/1IYLwVsEOQORsDH2eoJXpEP8_oWe8p…
#include <Eigen/Dense>
#include <chrono>
#include <iostream>
template <typename Func>
int run(int size, const std::string& name, Func&& f, bool print=false) {
Eigen::VectorXd D = Eigen::VectorXd::Random(size);
Eigen::VectorXd x = Eigen::VectorXd::Random(size);
using clock_type = std::chrono::steady_clock;
auto start = clock_type::now();
for(int i = 0; i < 1e4; ++i) {
x.noalias() = f(D,x);
}
auto end = clock_type::now();
auto duration = end - start;
auto dms = std::chrono::duration_cast<std::chrono::milliseconds>(duration);
if(print) {
std::cerr << name << "(" << size << "): " << dms.count() << "ms" << std::endl;
}
return dms.count();
}
int main() {
for(int i = 4; i < 40; ++i) {
int size = std::pow(2,i);
int ams = run(size, "asDiagonal", [](const Eigen::VectorXd& D, const Eigen::VectorXd& x) {
return D.asDiagonal() * x;
}, true);
int bms = run(size, "cwiseProduct",[](const Eigen::VectorXd& D, const Eigen::VectorXd& x) {
return D.cwiseProduct(x);
}, true);
std::cout << size << ", " << ams << ", " << bms << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment