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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <thread>
  5. #include "librf/librf.h"
  6. const size_t N = 5000000;
  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 : 平均 210字节
  32. //msvc : 平均600字节
  33. #if LIBRF_TUTORIAL_STAND_ALONE
  34. int main()
  35. {
  36. resumable_main_benchmark_mem(false);
  37. return 0;
  38. }
  39. #endif