-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
83 lines (66 loc) · 2.3 KB
/
test.cpp
File metadata and controls
83 lines (66 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <vector>
#include <chrono>
#include <numeric>
#include <thread>
#include <boost/asio.hpp>
#include "threadPool.h"
constexpr int num_tasks = 10000;
// 简单的计算任务
int compute_task(int n) {
int sum = 0;
int j=1;
for (int i = 0; i < 1000000; ++i) {
sum *= (i + n);
sum+=j;
j++;
}
return sum;
}
// 测试自定义线程池
void test_custom_thread_pool() {
ThreadPool pool(4); // 初始化时有4个线程
pool.init();
std::vector<std::future<int>> futures;
auto start_time = std::chrono::high_resolution_clock::now();
for (int i = 0; i < num_tasks; ++i) {
futures.emplace_back(pool.submit(compute_task, i));
}
int total_sum = 0;
for (auto& future : futures) {
total_sum += future.get(); // 等待所有任务完成
}
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << "Custom ThreadPool execution time: " << duration.count() << " milliseconds" << std::endl;
std::cout << "Total sum (Custom ThreadPool): " << total_sum << std::endl;
pool.shutdown();
}
// 测试 Boost.Asio 线程池
void test_boost_asio_thread_pool() {
boost::asio::thread_pool pool(4); // 4个线程
std::vector<std::future<int>> futures;
auto start_time = std::chrono::high_resolution_clock::now();
for (int i = 0; i < num_tasks; ++i) {
futures.emplace_back(std::async(std::launch::async, [&pool, i]() {
boost::asio::post(pool, []{});
return compute_task(i);
}));
}
int total_sum = 0;
for (auto& future : futures) {
total_sum += future.get(); // 等待所有任务完成
}
pool.join();
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
std::cout << "Boost.Asio ThreadPool execution time: " << duration.count() << " milliseconds" << std::endl;
std::cout << "Total sum (Boost.Asio ThreadPool): " << total_sum << std::endl;
}
int main() {
std::cout << "Testing Custom ThreadPool Performance...\n";
test_custom_thread_pool();
std::cout << "\nTesting Boost.Asio ThreadPool Performance...\n";
test_boost_asio_thread_pool();
return 0;
}