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

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