-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbench-iterator.cc
104 lines (93 loc) · 3.73 KB
/
bench-iterator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/iterator - STLIterator
// (c) Daniel Llorens - 2023
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include "ra/test.hh"
using std::cout, std::endl, std::flush, ra::TestRecorder, ra::Benchmark;
using real = double;
// FIXME Small cases get reduced statically by -O3 (at least gcc11-13), which isn't very useful.
int main()
{
TestRecorder tr(cout);
cout.precision(4);
tr.section("rank 1");
{
auto test1 = [&tr](char const * atag, auto && A, int N)
{
auto s = ra::size(A);
Benchmark bm { N, 3 };
auto report = [&](std::string const & tag, auto && bv)
{
tr.info(std::setw(5), std::fixed, bm.avg(bv)/s/1e-9, " ns [", bm.stddev(bv)/s/1e-9, "] ",
atag, " [", ra::noshape, ra::shape(A), "] ", tag)
.test_eq(ra::iota(s), A);
};
report("range for",
bm.run([&] {
int i = 0;
for (auto & a: A) {
a = i;
++i;
}
}));
report("ply undef",
bm.run([&] {
A = ra::_0;
}));
report("ply def",
bm.run([&] {
A = ra::iota(s);
}));
};
tr.section("static dimensions");
test1("small", ra::Small<real, 10>(), 50000000);
tr.section("static rank");
test1("unique", ra::Unique<real, 1>({10000}, ra::none), 5000);
test1("view", ra::Unique<real, 1>({10000}, ra::none).view(), 5000);
tr.section("var rank");
test1("unique", ra::Unique<real>({10000}, ra::none), 5000);
test1("view", ra::Unique<real>({10000}, ra::none).view(), 5000);
}
tr.section("rank 2");
{
auto test2 = [&tr](char const * atag, auto && A, int N)
{
auto s = ra::size(A);
Benchmark bm { N, 3 };
auto report = [&](std::string const & tag, auto && bv)
{
auto B = ra::Unique<real, 1>({s}, A.begin(), s);
tr.info(std::setw(5), std::fixed, bm.avg(bv)/s/1e-9, " ns [", bm.stddev(bv)/s/1e-9, "] ",
atag, " [", ra::noshape, ra::shape(A), "] ", tag)
.test_eq(ra::iota(s), B);
};
report("range for",
bm.run([&] {
int i = 0;
for (auto & a: A) {
a = i;
++i;
}
}));
};
tr.section("static dimensions");
test2("small", ra::Small<real, 10, 10>(), 1000000);
test2("small transposed", ra::transpose<1, 0>(ra::Small<real, 10, 10>()), 1000000);
tr.section("static rank");
test2("unique", ra::Unique<real, 2>({1000, 1000}, ra::none), 100);
test2("view", ra::Unique<real, 2>({1000, 1000}, ra::none).view(), 100);
test2("transposed view", ra::transpose<1, 0>(ra::Unique<real, 2>({1000, 1000}, ra::none)), 100);
tr.section("var rank");
test2("unique", ra::Unique<real>({1000, 1000}, ra::none), 100);
test2("view", ra::Unique<real>({1000, 1000}, ra::none).view(), 100);
test2("transposed view", ra::transpose<1, 0>(ra::Unique<real>({1000, 1000}, ra::none)), 100);
}
return tr.summary();
}