我有一个小程序,使用各种卡片计数策略实现了BlackJack的蒙特卡罗模拟。我的主要功能基本上是这样的:Boost.Thread没有加速?
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);
整个程序在我的机器上运行一个线程大约需要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();
然而,当我跑这个程序,即使我得到了相同的结果,但大约需要24秒才能完成。我在这里错过了什么吗?
您是否检查程序是否实际上使用了所有内核? – 2011-05-20 03:34:09