bench-pack.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // -*- mode: c++; coding: utf-8 -*-
  2. // ra-ra/bench - pack() operator vs explode/collapse.
  3. // (c) Daniel Llorens - 2016-2017
  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 "ra/test.hh"
  11. using std::cout, std::endl, std::flush, ra::TestRecorder, ra::Benchmark;
  12. using real = double;
  13. using complex = std::complex<double>;
  14. int main()
  15. {
  16. TestRecorder tr(cout);
  17. cout.precision(4);
  18. auto bench = [&tr](auto && f, auto A_, char const * tag, int size, int reps)
  19. {
  20. using A = decltype(A_);
  21. A a({size}, ra::none);
  22. Benchmark bm { reps, 3 };
  23. auto bv = bm.run([&]() { f(a, size); });
  24. tr.info(std::setw(5), std::fixed, bm.avg(bv)/size/1e-9, " ns [", bm.stddev(bv)/size/1e-9 ,"] ", tag)
  25. .test_eq(ra::pack<complex>(ra::iota(size, 0.), size-ra::iota(size, 0.)), a);
  26. };
  27. auto f_raw = [](auto & a, int size)
  28. {
  29. real * p = reinterpret_cast<real *>(a.data());
  30. for (ra::dim_t i=0; i!=size; ++i, p+=2) {
  31. p[0] = i;
  32. p[1] = size-i;
  33. }
  34. };
  35. auto f_reim = [](auto & a, int size)
  36. {
  37. real_part(a) = ra::iota(size);
  38. imag_part(a) = size-ra::iota(size);
  39. };
  40. auto f_collapse = [](auto & a, int size)
  41. {
  42. auto areim = ra::collapse<real>(a);
  43. areim(ra::all, 0) = ra::iota(size);
  44. areim(ra::all, 1) = size-ra::iota(size);
  45. };
  46. auto f_pack = [](auto & a, int size)
  47. {
  48. a = ra::pack<complex>(ra::iota(size, 0.), size-ra::iota(size, 0.));
  49. };
  50. auto f_xi = [](auto & a, int size)
  51. {
  52. a = ra::iota(size, 0.) + xi(size-ra::iota(size, 0.));
  53. };
  54. auto bench_all = [&](auto A_, int size, int n)
  55. {
  56. tr.section("size ", size, ", n ", n);
  57. bench(f_raw, A_, "raw", size, n);
  58. bench(f_reim, A_, "re/im", size, n);
  59. bench(f_collapse, A_, "collapse", size, n);
  60. bench(f_pack, A_, "pack", size, n);
  61. bench(f_xi, A_, "xi", size, n);
  62. };
  63. bench_all(ra::Big<complex, 1>(), 10, 1000000);
  64. bench_all(ra::Big<complex, 1>(), 100, 100000);
  65. bench_all(ra::Big<complex, 1>(), 1000, 10000);
  66. bench_all(ra::Big<complex, 1>(), 10000, 1000);
  67. bench_all(ra::Big<complex, 1>(), 100000, 100);
  68. bench_all(ra::Big<complex, 1>(), 1000000, 10);
  69. return tr.summary();
  70. }