Random Engine Differences

前端 未结 7 1366
灰色年华
灰色年华 2020-12-13 02:06

The C++11 standard specifies a number of different engines for random number generation: linear_congruential_engine, mersenne_twister_engine,

相关标签:
7条回答
  • 2020-12-13 02:31

    Some of the information in these other answers conflicts with my findings. I've run tests on Windows 8.1 using Visual Studio 2013, and consistently I've found mersenne_twister_engine to be but higher quality and significantly faster than either linear_congruential_engine or subtract_with_carry_engine. This leads me to believe, when the information in the other answers are taken into account, that the specific implementation of an engine has a significant impact on performance.

    This is of great surprise to nobody, I'm sure, but it's not mentioned in the other answers where mersenne_twister_engine is said to be slower. I have no test results for other platforms and compilers, but with my configuration, mersenne_twister_engine is clearly the superior choice when considering period, quality, and speed performance. I have not profiled memory usage, so I cannot speak to the space requirement property.

    Here's the code I'm using to test with (to make portable, you should only have to replace the windows.h QueryPerformanceXxx() API calls with an appropriate timing mechanism):

    // compile with: cl.exe /EHsc
    #include <random> 
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void test_lc(const int a, const int b, const int s) {
        /*
        typedef linear_congruential_engine<unsigned int, 48271, 0, 2147483647> minstd_rand;
        */
        minstd_rand gen(1729);
    
        uniform_int_distribution<> distr(a, b);
    
        for (int i = 0; i < s; ++i) {
            distr(gen);
        }
    }
    
    void test_mt(const int a, const int b, const int s) {
        /*
        typedef mersenne_twister_engine<unsigned int, 32, 624, 397,
        31, 0x9908b0df,
        11, 0xffffffff,
        7, 0x9d2c5680,
        15, 0xefc60000,
        18, 1812433253> mt19937;
        */
        mt19937 gen(1729);
    
        uniform_int_distribution<> distr(a, b);
    
        for (int i = 0; i < s; ++i) {
            distr(gen);
        }
    }
    
    void test_swc(const int a, const int b, const int s) {
        /*
        typedef subtract_with_carry_engine<unsigned int, 24, 10, 24> ranlux24_base;
        */
        ranlux24_base gen(1729);
    
        uniform_int_distribution<> distr(a, b);
    
        for (int i = 0; i < s; ++i) {
            distr(gen);
        }
    }
    
    int main()
    {
        int a_dist = 0;
        int b_dist = 1000;
    
        int samples = 100000000;
    
        cout << "Testing with " << samples << " samples." << endl;
    
        LARGE_INTEGER ElapsedTime;
        double        ElapsedSeconds = 0;
    
        LARGE_INTEGER Frequency;
        QueryPerformanceFrequency(&Frequency);
        double TickInterval = 1.0 / ((double) Frequency.QuadPart);
    
        LARGE_INTEGER StartingTime;
        LARGE_INTEGER EndingTime;
        QueryPerformanceCounter(&StartingTime);
        test_lc(a_dist, b_dist, samples);
        QueryPerformanceCounter(&EndingTime);
        ElapsedTime.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
        ElapsedSeconds = ElapsedTime.QuadPart * TickInterval;
        cout << "linear_congruential_engine time: " << ElapsedSeconds << endl;
    
        QueryPerformanceCounter(&StartingTime);
        test_mt(a_dist, b_dist, samples);
        QueryPerformanceCounter(&EndingTime);
        ElapsedTime.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
        ElapsedSeconds = ElapsedTime.QuadPart * TickInterval;
        cout << "   mersenne_twister_engine time: " << ElapsedSeconds << endl;
    
        QueryPerformanceCounter(&StartingTime);
        test_swc(a_dist, b_dist, samples);
        QueryPerformanceCounter(&EndingTime);
        ElapsedTime.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
        ElapsedSeconds = ElapsedTime.QuadPart * TickInterval;
        cout << "subtract_with_carry_engine time: " << ElapsedSeconds << endl;
    }
    

    Output:

    Testing with 100000000 samples.
    linear_congruential_engine time: 10.0821
       mersenne_twister_engine time: 6.11615
    subtract_with_carry_engine time: 9.26676
    
    0 讨论(0)
提交回复
热议问题