基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
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_async_mem.cpp 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include "librf/librf.h"
  6. const size_t N = 10'000'000;
  7. const size_t LOOP_COUNT = 50;
  8. std::atomic<size_t> globalValue{0};
  9. void resumable_main_benchmark_mem(bool wait_key)
  10. {
  11. using namespace std::chrono;
  12. for (size_t i = 0; i < N; ++i)
  13. {
  14. go[=]()->librf::generator_t<size_t>
  15. {
  16. for (size_t k = 0; k < LOOP_COUNT; ++k)
  17. {
  18. globalValue += i * k;
  19. co_yield k;
  20. }
  21. co_return 0;
  22. };
  23. }
  24. librf::this_scheduler()->run_until_notask();
  25. if (wait_key)
  26. {
  27. std::cout << "press any key to continue." << std::endl;
  28. (void)getchar();
  29. }
  30. }
  31. //clang :
  32. // x64: 平均 256 字节, operator new: size = 48, state size = 32
  33. // x86: 平均 121 字节, operator new: size = 40, state size = 16
  34. //msvc : 平均 304 字节(vs2022,17.7.4)
  35. // x64: 平均 304 字节, operator new: size = 144, state size = 32
  36. // x86: 平均 153 字节, operator new: size = 72, state size = 16
  37. #if LIBRF_TUTORIAL_STAND_ALONE
  38. int main()
  39. {
  40. resumable_main_benchmark_mem(false);
  41. return 0;
  42. }
  43. #endif