Skip to content

Instantly share code, notes, and snippets.

@timmyshen
Created August 29, 2014 02:49
Show Gist options
  • Save timmyshen/0c84a86821a8b025a809 to your computer and use it in GitHub Desktop.
Save timmyshen/0c84a86821a8b025a809 to your computer and use it in GitHub Desktop.

Revisions

  1. timmyshen created this gist Aug 29, 2014.
    61 changes: 61 additions & 0 deletions source.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #include <iostream>
    #include <chrono>
    #include <random>

    int main(int argc, char* argv [])
    {
    auto x = 1;
    std::cout << "auto variable x = " << x << std::endl;

    srand(0u);
    auto r = rand();
    std::cout << "rand() = " << r << std::endl;

    std::cout << "next rand() = " << rand() << std::endl;

    auto start = std::chrono::steady_clock::now();
    int sum(0);
    for (size_t i = 0; i < 10000000; i++)
    {
    sum += rand();
    }
    auto end = std::chrono::steady_clock::now();
    auto span = end - start;
    std::cout << "time span in seconds: " << span.count() << std::endl;
    std::cout << "sum = " << sum << std::endl;


    start = std::chrono::steady_clock::now();
    std::default_random_engine rand_eng;
    std::normal_distribution<double> normal_std(0.0, 1.0);

    auto rd = normal_std(rand_eng);
    std::cout << "random double = " << rd << std::endl;

    double dsum(0.0);
    for (size_t i = 0; i < 10000000; i++)
    {
    dsum += normal_std(rand_eng);
    }
    end = std::chrono::steady_clock::now();
    span = end - start;
    std::cout << "time span in seconds: " << span.count() << std::endl;
    std::cout << "dsum = " << dsum << std::endl;


    start = std::chrono::steady_clock::now();
    std::default_random_engine rand_eng1;

    double dsum2(0.0);
    for (size_t i = 0; i < 10000000; i++)
    {
    std::normal_distribution<double> normal_std(0.0, 1.0);
    dsum2 += normal_std(rand_eng1);
    }
    end = std::chrono::steady_clock::now();
    span = end - start;
    std::cout << "time span in seconds: " << span.count() << std::endl;
    std::cout << "dsum2 = " << dsum2 << std::endl;

    return EXIT_SUCCESS;
    }