30 std::cout <<
"=== Basic async_executor Usage ===\n";
35 std::cout <<
"Executor created with " << executor.
thread_count() <<
" threads\n";
39 std::cout <<
"Submitting 5 simple tasks...\n";
40 std::vector<std::future<int>> futures;
42 auto start = high_resolution_clock::now();
44 for (
int i = 0; i < 5; ++i) {
45 auto future = executor.
submit([i]() {
47 std::this_thread::sleep_for(milliseconds(100));
50 futures.push_back(std::move(future));
54 std::cout <<
"Collecting results:\n";
55 for (
size_t i = 0; i < futures.size(); ++i) {
56 int result = futures[i].get();
57 std::cout <<
" Task " << i <<
" result: " << result <<
"\n";
60 auto end = high_resolution_clock::now();
61 auto duration = duration_cast<milliseconds>(end - start);
63 std::cout <<
"Total time: " << duration.count() <<
"ms\n";
64 std::cout <<
"(Expected ~100ms with concurrent execution)\n\n";
68 std::cout <<
"=== High-Throughput Performance Test ===\n";
72 const int num_tasks = 10000;
73 std::cout <<
"Submitting " << num_tasks <<
" lightweight tasks...\n";
75 auto start = high_resolution_clock::now();
77 std::vector<std::future<int>> futures;
78 futures.reserve(num_tasks);
80 for (
int i = 0; i < num_tasks; ++i) {
81 futures.push_back(executor.
submit([i]() {
90 auto end = high_resolution_clock::now();
91 auto duration = duration_cast<microseconds>(end - start);
93 std::cout <<
"Submitted " << num_tasks <<
" tasks in "
94 << duration.count() <<
" microseconds\n";
95 std::cout <<
"Average latency: "
96 << (duration.count() / num_tasks) <<
" microseconds/task\n";
99 std::cout <<
"â
thread_system: Expected ~77ns latency per task\n";
101 std::cout <<
"â ïļ Fallback mode: Expected ~2-5Ξs latency per task\n";
159 std::cout <<
"=== Performance Comparison ===\n";
160 std::cout <<
"async_executor vs std::async\n\n";
162 const int num_tasks = 1000;
167 auto start = high_resolution_clock::now();
169 std::vector<std::future<int>> futures;
170 for (
int i = 0; i < num_tasks; ++i) {
171 futures.push_back(executor.
submit([i]() { return i * 2; }));
174 for (
auto& f : futures) {
178 auto end = high_resolution_clock::now();
179 auto duration = duration_cast<microseconds>(end - start);
181 std::cout <<
"async_executor: " << duration.count() <<
" Ξs\n";
182 std::cout <<
" (" << (duration.count() / num_tasks) <<
" Ξs/task)\n";
187 auto start = high_resolution_clock::now();
189 std::vector<std::future<int>> futures;
190 for (
int i = 0; i < num_tasks; ++i) {
191 futures.push_back(std::async(std::launch::async, [i]() {
return i * 2; }));
194 for (
auto& f : futures) {
198 auto end = high_resolution_clock::now();
199 auto duration = duration_cast<microseconds>(end - start);
201 std::cout <<
"std::async: " << duration.count() <<
" Ξs\n";
202 std::cout <<
" (" << (duration.count() / num_tasks) <<
" Ξs/task)\n";