Eterfree线程池以一个基于Boost程序库的线程池为参照线程池,分别在不同硬件、不同操作系统的计算机对两种线程池进行性能测试。
环境
用于性能测试的四台计算机配置信息如表所示:
CPU型号 | CPU核心频率 | CPU核心数量 | CPU线程数量 | 内存容量 | 操作系统 | 系统位数 |
---|---|---|---|---|---|---|
Intel Core i3-3110M | 2.4GHz | 2 | 4 | 4GB | Windows 8.0 专业版 | 64 |
Intel Core i5-6200U | 2.3-2.8GHz | 2 | 4 | 8GB | Windows 10 家庭中文版 | 64 |
AMD Ryzen 3 PRO 1200 | 3.1-3.4GHz | 4 | 4 | 8GB | Windows 10 企业版 | 64 |
AMD Ryzen 7 4800H | 2.9-4.2GHz | 8 | 16 | 16GB | Windows 10 家庭中文版 | 64 |
方法
最近一次以Visual Studio 2022编译x64 Release版本的测试程序,一共进行10次测试,每次测试以16个线程执行100000个相同任务。记录线程池执行的任务数和所用时间,计算每毫秒执行的任务数,最终取执行任务的平均速度。
任务
任务代码如下所示:
#include <chrono>
#include <string>
#include <iostream>
#include <atomic>
#include <thread>
#ifdef _WIN32
#include <Windows.h>
#pragma comment(lib, "WinMM.Lib")
#endif // _WIN32
static constexpr std::chrono::nanoseconds::rep SLEEP_TIME = 1000000;
static std::atomic_ulong counter = 0;
static void sleepFor(std::chrono::nanoseconds::rep _duration)
{
#ifdef _WIN32
constexpr UINT PERIOD = 1;
auto result = ::timeBeginPeriod(PERIOD);
if (result != TIMERR_NOERROR)
{
std::string buffer = "timeBeginPeriod error ";
buffer += std::to_string(result);
std::cerr << buffer << std::endl;
}
#endif // _WIN32
auto duration = std::chrono::nanoseconds(_duration);
std::this_thread::sleep_for(duration);
#ifdef _WIN32
result = ::timeEndPeriod(PERIOD);
if (result != TIMERR_NOERROR)
{
std::string buffer = "timeEndPeriod error ";
buffer += std::to_string(result);
std::cerr << buffer << std::endl;
}
#endif // _WIN32
}
static void task()
{
for (volatile auto index = 0UL; \
index < 10000UL; ++index);
sleepFor(SLEEP_TIME);
counter.fetch_add(1, \
std::memory_order_relaxed);
}
源码
测试代码分布于以下源文件:
Common.cpp
- GitCode: Common.cpp
- Gitee: Common.cpp
- GitHub: Common.cpp
Eterfree.cpp
- GitCode: Eterfree.cpp
- Gitee: Eterfree.cpp
- GitHub: Eterfree.cpp
结果
记录最近一次的测试数据,Eterfree线程池的测试数据如表所示:
测试次数 | 任务数量(个) | 运行时间(毫秒) | 执行速度(个/毫秒) |
---|---|---|---|
1 | 80093 | 10009 | 8.0021 |
2 | 80136 | 10008 | 8.00719 |
3 | 80147 | 10009 | 8.00749 |
4 | 80102 | 10007 | 8.0046 |
5 | 80170 | 10010 | 8.00899 |
6 | 80160 | 10009 | 8.00879 |
7 | 80170 | 10008 | 8.01059 |
8 | 80188 | 10009 | 8.01159 |
9 | 80172 | 10009 | 8.00999 |
10 | 80149 | 10010 | 8.00689 |
平均 | 80148.7 | 10008.8 | 8.00782 |
测试结果显示,Eterfree线程池平均每毫秒执行8.00782个任务。Eterfree线程池与参照线程池在伯仲之间,或许在性能上略优于参照线程池。