You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

benchmark-shared-unique.cpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "benchmark-shared-unique.h"
  2. #include "measure_time.h"
  3. #include "make_unique.h"
  4. #define COUNT (1u << 26)
  5. void benchmark_forward_empty_raw() {
  6. for (size_t i = 0; i < COUNT; ++i) {
  7. (void) forward_raw(nullptr);
  8. }
  9. }
  10. void benchmark_forward_empty_unique() {
  11. for (size_t i = 0; i < COUNT; ++i) {
  12. (void) forward_unique({});
  13. }
  14. }
  15. void benchmark_forward_empty_shared() {
  16. for (size_t i = 0; i < COUNT; ++i) {
  17. (void) forward_shared({});
  18. }
  19. }
  20. void benchmark_forward_empty_shared_from_unique() {
  21. for (size_t i = 0; i < COUNT; ++i) {
  22. (void) forward_shared(std::unique_ptr<size_t>{});
  23. }
  24. }
  25. void benchmark_forward_new_raw() {
  26. for (size_t i = 0; i < COUNT; ++i) {
  27. size_t* p = new size_t{i};
  28. (void) forward_raw(p);
  29. delete p;
  30. }
  31. }
  32. void benchmark_forward_new_unique() {
  33. for (size_t i = 0; i < COUNT; ++i) {
  34. (void) forward_unique(std::make_unique<size_t>(i));
  35. }
  36. }
  37. void benchmark_forward_new_shared() {
  38. for (size_t i = 0; i < COUNT; ++i) {
  39. (void) forward_shared(std::make_shared<size_t>(i));
  40. }
  41. }
  42. void benchmark_forward_new_shared_from_unique() {
  43. for (size_t i = 0; i < COUNT; ++i) {
  44. (void) forward_shared(std::make_unique<size_t>(i));
  45. }
  46. }
  47. void benchmark_forward_new_raw_large() {
  48. for (size_t i = 0; i < COUNT; ++i) {
  49. Large* p = new Large{i};
  50. (void) forward_raw(p);
  51. delete p;
  52. }
  53. }
  54. void benchmark_forward_new_unique_large() {
  55. for (size_t i = 0; i < COUNT; ++i) {
  56. (void) forward_unique_large(std::make_unique<Large>(i));
  57. }
  58. }
  59. void benchmark_forward_new_shared_large() {
  60. for (size_t i = 0; i < COUNT; ++i) {
  61. (void) forward_shared_large(std::make_shared<Large>(i));
  62. }
  63. }
  64. void benchmark_forward_new_shared_large_from_unique() {
  65. for (size_t i = 0; i < COUNT; ++i) {
  66. (void) forward_shared_large(std::make_unique<Large>(i));
  67. }
  68. }
  69. int main() {
  70. std::cout << "forward empty raw : ";
  71. measure_time(benchmark_forward_empty_raw);
  72. std::cout << "forward empty unique : ";
  73. measure_time(benchmark_forward_empty_unique);
  74. std::cout << "forward empty shared : ";
  75. measure_time(benchmark_forward_empty_shared);
  76. std::cout << "forward empty shared from unique : ";
  77. measure_time(benchmark_forward_empty_shared_from_unique);
  78. std::cout << "\n";
  79. std::cout << "forward new raw : ";
  80. measure_time(benchmark_forward_new_raw);
  81. std::cout << "forward new unique : ";
  82. measure_time(benchmark_forward_new_unique);
  83. std::cout << "forward new shared : ";
  84. measure_time(benchmark_forward_new_shared);
  85. std::cout << "forward new shared from unique : ";
  86. measure_time(benchmark_forward_new_shared_from_unique);
  87. std::cout << "\n";
  88. std::cout << "forward new raw large : ";
  89. measure_time(benchmark_forward_new_raw_large);
  90. std::cout << "forward new unique large : ";
  91. measure_time(benchmark_forward_new_unique_large);
  92. std::cout << "forward new shared large : ";
  93. measure_time(benchmark_forward_new_shared_large);
  94. std::cout << "forward new shared large from unique: ";
  95. measure_time(benchmark_forward_new_shared_large_from_unique);
  96. }