基于C++ Coroutines提案 ‘Stackless Resumable Functions’编写的协程库
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

test_async_resumable.cpp 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <chrono>
  2. #include <iostream>
  3. #include <string>
  4. #include <conio.h>
  5. #include "librf.h"
  6. //static const int N = 100000000;
  7. static const int N = 10;
  8. template <typename T>
  9. void dump(std::string name, int n, T start, T end)
  10. {
  11. auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
  12. std::cout << name << " " << n << " " << ns << " ns " << ns / n << " ns/op" << std::endl;
  13. }
  14. auto yield_switch(int coro)
  15. {
  16. for (int i = 0; i < N / coro; ++i)
  17. co_yield i;
  18. return N / coro;
  19. }
  20. void resumable_switch(int coro)
  21. {
  22. auto start = std::chrono::steady_clock::now();
  23. for (int i = 0; i < coro; ++i)
  24. {
  25. //go yield_switch(coro);
  26. go [=]
  27. {
  28. for (int i = 0; i < N / coro; ++i)
  29. co_yield i;
  30. return N / coro;
  31. };
  32. }
  33. resumef::this_scheduler()->run_until_notask();
  34. auto end = std::chrono::steady_clock::now();
  35. dump("BenchmarkSwitch_" + std::to_string(coro), N, start, end);
  36. }
  37. void resumable_main_resumable()
  38. {
  39. resumable_switch(1);
  40. resumable_switch(1000);
  41. resumable_switch(1000000);
  42. }