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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 = 20000;
  12. using int_channel_ptr = std::shared_ptr<channel_t<intptr_t>>;
  13. static future_t<> passing_next(int_channel_ptr rd, int_channel_ptr wr)
  14. {
  15. for (;;)
  16. {
  17. intptr_t value = co_await *rd;
  18. co_await wr->write(value + 1);
  19. }
  20. }
  21. void benchmark_main_channel_passing_next()
  22. {
  23. int_channel_ptr head = std::make_shared<channel_t<intptr_t>>(1);
  24. int_channel_ptr in = head;
  25. int_channel_ptr tail = nullptr;
  26. for (int i = 0; i < MaxNum; ++i)
  27. {
  28. tail = std::make_shared<channel_t<intptr_t>>(1);
  29. go passing_next(in, tail);
  30. in = tail;
  31. }
  32. GO
  33. {
  34. for (;;)
  35. {
  36. auto tstart = high_resolution_clock::now();
  37. co_await (*head << 0);
  38. intptr_t value = co_await *tail;
  39. auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
  40. std::cout << value << " cost time " << dt << "s" << std::endl;
  41. }
  42. };
  43. this_scheduler()->run_until_notask();
  44. }