Created
March 2, 2016 11:01
-
-
Save telmin/846e1ee9ecddd4fcc063 to your computer and use it in GitHub Desktop.
This file contains 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
#include <iostream> | |
#include <vector> | |
#include <random> | |
#include <chrono> | |
template <typename T> | |
static void init(std::vector<T>& v) | |
{ | |
std::mt19937 mt(0); | |
for(auto& it : v) { | |
it = static_cast<T>(mt()); | |
} | |
} | |
template <typename T> | |
static double axpy(std::vector<T>& r) | |
{ | |
const auto num = r.size(); | |
constexpr size_t loop_count = 100; | |
double ret = 0.0; | |
std::vector<T> A(num); | |
std::vector<T> x(num); | |
std::vector<T> b(num); | |
init<T>(A); | |
init<T>(x); | |
init<T>(b); | |
for(auto l = decltype(loop_count)(0); l < loop_count; ++l) { | |
auto start = std::chrono::system_clock::now(); | |
for(auto i = decltype(num)(0); i < num; ++i) { | |
r[i] = A[i] * x[i] + b[i]; | |
} | |
auto end = std::chrono::system_clock::now(); | |
ret += std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count(); | |
} | |
// ret /= static_cast<double>(loop_count); | |
return ret; | |
} | |
int main() | |
{ | |
constexpr size_t num = 4096; | |
std::vector<float> vf(num); | |
std::vector<double> vd(num); | |
auto float_time = axpy<float>(vf); | |
auto double_time = axpy<double>(vd); | |
for(size_t i = 0; i < 10; ++i) { | |
std::cout << vf[i] << " " << vd[i] << std::endl; | |
} | |
std::cout << "float time " << float_time << " nsec" << std::endl; | |
std::cout << "double time " << double_time << " nsec " << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment