Hi all,
i've written a small code for estimating the mean from a series of
random numbers.
The random numbers are generated with and without boost::thread.
The simulation shows me that with boost::thread you only get a small
performance.
So my question is, how much performance can you get for simulation with
boost::thread?
with best regards,
Kim Tang
My code is shown below:
#include <iostream>
#include <fstream>
#include <ctime> // std::time
#include <climits>
#include
#include
#include
#include
#include
#include
#include
// Sun CC doesn't handle boost::iterator_adaptor yet
#if !defined(__SUNPRO_CC) || (__SUNPRO_CC > 0x530)
#include
#endif
#ifdef BOOST_NO_STDC_NAMESPACE
namespace std {
using ::time;
}
#endif
typedef boost::minstd_rand base_generator_type;
typedef boost::variate_generator > NormalRandomNumber;
class SimClass
{
public: SimClass(double& data,NormalRandomNumber & generator,
std::size_t number)
: data_(data),
generator_(generator),
number_(number)
{};
void operator()()
{
data_=0.0;
for(std::size_t i = 0; i < number_; i++)
data_+=generator_();
};
private:
double& data_;
NormalRandomNumber generator_;
std::size_t number_;
};
int main()
{
double mean=0.0;
const std::size_t NUM_OF_THREADS=4;
const std::size_t NUM_CALCS=10000000;
const std::size_t NSIMULATION=NUM_CALCS*NUM_OF_THREADS;
base_generator_type generator(42u);
std::cout < norm_dist(0,1);
NormalRandomNumber normalRN(generator, norm_dist);
std::cout.setf(std::ios::fixed);
// You can now retrieve random numbers from that distribution by means
// of a STL Generator interface, i.e. calling the generator as a zero-
// argument function.
boost::timer cTime;
for(int i = 0; i < NSIMULATION; i++)
mean+=normalRN();
std::cout <<"estimated mean is: " <