bench-iterator.cc 3.7 KB

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