small_obj.cpp 924 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "core/small_obj.hpp"
  2. #include <benchmark/benchmark.h>
  3. #include <memory>
  4. template<size_t length>
  5. static void BM_std_shared_ptr(benchmark::State &state) {
  6. struct test_type {
  7. double data[length];
  8. };
  9. for (auto _: state) {
  10. benchmark::DoNotOptimize(std::make_shared<test_type>());
  11. }
  12. }
  13. BENCHMARK(BM_std_shared_ptr<0>);
  14. BENCHMARK(BM_std_shared_ptr<1>);
  15. BENCHMARK(BM_std_shared_ptr<8>);
  16. BENCHMARK(BM_std_shared_ptr<16>);
  17. BENCHMARK(BM_std_shared_ptr<128>);
  18. template<size_t length>
  19. static void BM_small_obj(benchmark::State &state) {
  20. struct test_type : public sophiar::small_obj<test_type> {
  21. double data[length];
  22. };
  23. for (auto _: state) {
  24. benchmark::DoNotOptimize(test_type::new_instance());
  25. }
  26. }
  27. BENCHMARK(BM_small_obj<0>);
  28. BENCHMARK(BM_small_obj<1>);
  29. BENCHMARK(BM_small_obj<8>);
  30. BENCHMARK(BM_small_obj<16>);
  31. BENCHMARK(BM_small_obj<128>);
  32. BENCHMARK_MAIN();