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

test_async_resumable.cpp 1.7KB

hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
hace 6 años
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 
  2. #include <chrono>
  3. #include <iostream>
  4. #include <string>
  5. #include <conio.h>
  6. #include "librf.h"
  7. static const intptr_t N = 10000000;
  8. //static const int N = 10;
  9. static std::mutex lock_console;
  10. template <typename T>
  11. void dump(size_t idx, std::string name, T start, T end)
  12. {
  13. lock_console.lock();
  14. auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count();
  15. std::cout << idx << ":" << name << " ";
  16. std::cout << N << " " << ns << " ns ";
  17. std::cout << (ns / N) << " ns/op" << " ";
  18. std::cout << (N * 100 * 1000 / ns) << "w/cps" << std::endl;
  19. lock_console.unlock();
  20. }
  21. auto yield_switch(intptr_t coro)
  22. {
  23. for (intptr_t i = 0; i < N / coro; ++i)
  24. co_yield i;
  25. return N / coro;
  26. }
  27. void resumable_switch(intptr_t coro, size_t idx)
  28. {
  29. resumef::local_scheduler ls;
  30. auto start = std::chrono::steady_clock::now();
  31. for (intptr_t i = 0; i < coro; ++i)
  32. {
  33. //go yield_switch(coro);
  34. go [=]
  35. {
  36. for (intptr_t i = 0; i < N / coro; ++i)
  37. co_yield i;
  38. return N / coro;
  39. };
  40. }
  41. resumef::this_scheduler()->run_until_notask();
  42. auto end = std::chrono::steady_clock::now();
  43. dump(idx, "BenchmarkSwitch_" + std::to_string(coro), start, end);
  44. }
  45. void resumable_main_resumable()
  46. {
  47. //resumable_switch(1, 0);
  48. //resumable_switch(10, 0);
  49. //resumable_switch(100, 0);
  50. //resumable_switch(10000, 0);
  51. //resumable_switch(10000000, 0);
  52. std::thread works[32];
  53. for (size_t w = 1; w <= _countof(works); ++w)
  54. {
  55. for (size_t idx = 0; idx < w; ++idx)
  56. works[idx] = std::thread(&resumable_switch, 1000, idx);
  57. for (size_t idx = 0; idx < w; ++idx)
  58. works[idx].join();
  59. std::cout << std::endl << std::endl;
  60. }
  61. }