//----------------------------------------------------------------------------- // axb.cc //----------------------------------------------------------------------------- #include #include #include #include #include #include //----------------------------------------------------------------------------- const int M = 1234, N = 678; // multiplos de 2 por quitar if y desenrollar static_assert(M % 2 == 0 && N % 2 == 0); //----------------------------------------------------------------------------- int a[N][M], b[N][M], c[N]; //----------------------------------------------------------------------------- void f0() // original { for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) if ((j % 2) == 0) c[j] += a[j][i] + b[j][i]; else c[j] += a[j][i] - b[j][i]; } //----------------------------------------------------------------------------- template void test(const F& f, const char* name) { const unsigned REP = 33; std::chrono::duration rep[REP]; std::ranges::fill(c, 0); for (auto& i: rep) { auto start = std::chrono::high_resolution_clock::now(); f(); auto stop = std::chrono::high_resolution_clock::now(); i = stop - start; } std::nth_element(rep, rep + REP / 2, rep + REP); std::cout << std::setw(16) << name << ':' << " time: " << std::fixed << std::setprecision(2) << std::setw(10) << rep[REP / 2].count() << "us" << " result = " << std::accumulate(std::begin(c), std::end(c), 0) << std::endl; } //----------------------------------------------------------------------------- int main() { std::random_device device; std::default_random_engine engine(device()); std::uniform_int_distribution distribution(-5, 5); auto rng = std::bind(distribution, engine); for (int i = 0; i < N; ++i) for (int j = 0; j < M; ++j) { a[i][j] = rng(); b[i][j] = rng(); } test(f0, "original"); } //-----------------------------------------------------------------------------