50 lines
1.1 KiB
C++
50 lines
1.1 KiB
C++
#include <iostream>
|
|
#include <random>
|
|
#include <csignal>
|
|
#include "pool/ThreadPool.h"
|
|
std::random_device rd; // 真实随机数产生器
|
|
|
|
std::mt19937 mt(rd()); //生成计算随机数mt
|
|
|
|
std::uniform_int_distribution<int> dist(-1000, 1000); //生成-1000到1000之间的离散均匀分布数
|
|
|
|
auto rnd = std::bind(dist, mt);
|
|
|
|
// 添加两个数字的简单函数并打印结果
|
|
|
|
void multiply(const int a, const int b)
|
|
{
|
|
const int res = a * b;
|
|
}
|
|
|
|
int main() {
|
|
ThreadPool* pool = new ThreadPool(10);
|
|
|
|
time_t time1 = 0;
|
|
time_t time2 = 0;
|
|
|
|
for (int i = 0; i < 1000000; ++i) {
|
|
time_t begin = time(NULL);
|
|
pool->pushTask(multiply, rnd(), rnd());
|
|
time_t end = time(NULL);
|
|
time1 += end -begin;
|
|
}
|
|
|
|
for (int i = 0; i < 1000000; ++i) {
|
|
int a = rnd();
|
|
int b = rnd();
|
|
time_t begin = time(NULL);
|
|
std::thread t = std::thread([&]() {
|
|
const int res = a * b;
|
|
});
|
|
t.join();
|
|
time_t end = time(NULL);
|
|
time2 += end - begin;
|
|
}
|
|
|
|
std::cout << time1 << std::endl;
|
|
std::cout << time2 << std::endl;
|
|
|
|
return 0;
|
|
}
|