基于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_channel_passing_next.cpp 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <thread>
  6. #include <deque>
  7. #include <mutex>
  8. #include "librf.h"
  9. using namespace resumef;
  10. using namespace std::chrono;
  11. using namespace std::literals;
  12. const static auto MaxNum = 100000;
  13. const static auto LoopCount = 100;
  14. using int_channel_ptr = std::shared_ptr<channel_t<intptr_t>>;
  15. static future_t<> passing_next(channel_t<intptr_t> rd, channel_t<intptr_t> wr)
  16. {
  17. for (int i = 0; i < LoopCount; ++i)
  18. {
  19. intptr_t value = co_await rd;
  20. co_await wr.write(value + 1);
  21. }
  22. }
  23. void benchmark_main_channel_passing_next()
  24. {
  25. channel_t<intptr_t> head{1};
  26. channel_t<intptr_t> in = head;
  27. channel_t<intptr_t> tail{0};
  28. for (int i = 0; i < MaxNum; ++i)
  29. {
  30. tail = channel_t<intptr_t>{ 1 };
  31. go passing_next(in, tail);
  32. in = tail;
  33. }
  34. GO
  35. {
  36. for (int i = 0; i < LoopCount; ++i)
  37. {
  38. auto tstart = high_resolution_clock::now();
  39. co_await (head << 0);
  40. intptr_t value = co_await tail;
  41. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  42. std::cout << value << " cost time " << dt << "s" << std::endl;
  43. }
  44. };
  45. this_scheduler()->run_until_notask();
  46. }