基于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.2KB

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