123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <cstdlib>
- #include "ra/bench.hh"
- using std::cout, std::endl, std::flush, ra::TestRecorder;
- using real = double;
- using ra::dim_t;
- int main()
- {
- ra::TestRecorder tr(std::cout);
- auto test = [&tr](auto && C, auto && I, int reps)
- {
- int M = C.len(0);
- int N = C.len(1);
- int O = I.len(0);
- C = 4*ra::_0 + ra::_1;
- I(ra::all, 0) = map([&](auto && i) { return i%M; }, ra::_0 + (std::rand() & 1));
- I(ra::all, 1) = map([&](auto && i) { return i%N; }, ra::_0 + (std::rand() & 1));
- int ref0 = sum(at(C, iter<1>(I))), val0 = 0;
- Benchmark bm { reps, 3 };
- auto report = [&](std::string const & tag, auto && bv)
- {
- tr.info(std::setw(5), std::fixed, bm.avg(bv)/M/N/1e-9, " ns [", bm.stddev(bv)/M/N/1e-9, "] ", tag)
- .test_eq(val0, ref0);
- };
- report("direct subscript",
- bm.run([&] {
- int val = 0;
- for (int i=0; i<O; ++i) {
- val += C(dim_t(I(i, 0)), dim_t(I(i, 1)));
- }
- val0 = val;
- }));
- report("at member + loop",
- bm.run([&] {
- int val = 0;
- for (int i=0; i<O; ++i) {
- val += C.at(I(i));
- }
- val0 = val;
- }));
- report("at op + iter",
- bm.run([&] {
- val0 = sum(at(C, iter<1>(I)));
- }));
- };
- tr.section("Bigs/Bigs");
- {
- ra::Big<int, 2> C({1000, 4}, ra::none);
- ra::Big<int, 2> I({1000, 2}, ra::none);
- test(C, I, 100);
- }
- tr.section("Bigd/Bigd");
- {
- ra::Big<int> C({1000, 4}, ra::none);
- ra::Big<int> I({1000, 2}, ra::none);
- test(C, I, 100);
- }
- tr.section("Small/Small");
- {
- ra::Small<int, 10, 4> C;
- ra::Small<int, 10, 2> I;
- test(C, I, 10000);
- }
- tr.section("Bigd/Bigs");
- {
- ra::Big<int> C({1000, 4}, ra::none);
- ra::Big<int, 2> I({1000, 2}, ra::none);
- test(C, I, 100);
- }
- tr.section("Bigs/Bigd");
- {
- ra::Big<int, 2> C({1000, 4}, ra::none);
- ra::Big<int> I({1000, 2}, ra::none);
- test(C, I, 100);
- }
- tr.section("Bigs/Small");
- {
- ra::Big<int, 2> C({1000, 4}, ra::none);
- ra::Small<int, 10, 2> I;
- test(C, I, 1000);
- }
- tr.section("Bigd/Small");
- {
- ra::Big<int> C({1000, 4}, ra::none);
- ra::Small<int, 10, 2> I;
- test(C, I, 1000);
- }
-
-
-
-
-
-
- return tr.summary();
- }
|