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

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