我有一个小程序,使用各种卡计数策略实现BlackJack的蒙特卡罗模拟.我的主要功能基本上是这样的:
int bankroll = 50000;
int hands = 100;
int tests = 10000;
Simulation::strategy = hi_lo;
for(int i = 0; i < simulations; ++i)
runSimulation(bankroll, hands, tests, strategy);
Run Code Online (Sandbox Code Playgroud)
整个程序在我的机器上的单个线程中运行大约需要10秒钟.
我想利用我的处理器所拥有的3个内核,所以我决定重写程序,只需在不同的线程中执行各种策略,如下所示:
int bankroll = 50000;
int hands = 100;
int tests = 10000;
Simulation::strategy = hi_lo;
boost::thread threads[simulations];
for(int i = 0; i < simulations; ++i)
threads[i] = boost::thread(boost::bind(runSimulation, bankroll, hands, tests, strategy));
for(int i = 0; i < simulations; ++i)
threads[i].join();
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此程序时,即使我得到相同的结果,也需要大约24秒才能完成.我在这里错过了什么吗?
如果值simulations很高,那么最终会创建大量线程,这样做的开销最终会破坏任何可能的性能提升.
编辑:一种方法可能是只启动三个线程,让它们各自运行所需模拟的1/3.或者,使用某种线程池也可以提供帮助.
| 归档时间: |
|
| 查看次数: |
273 次 |
| 最近记录: |