基于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

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