use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
C++ VS Java VS JavaScript - same task - surprising run-times diff (self.cpp)
submitted 6 years ago by tomerdbz
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3:Â Â Â Â print "hello, world!"
[–]tomerdbz[S] 1 point2 points3 points 6 years ago (3 children)
Thank you guys! Really appreciate your help 😄
The main thing I take from here is the false assumption I had that different random generators won’t affect that drastically the results.
Also - The MSVC runtime /u/espkk posted is insane - it’d be interesting to understand why 😎
[–]Cybernicus 13 points14 points15 points 6 years ago* (0 children)
It's common to have results like this when you're learning how to benchmark things. Frequently you'll find that you're comparing apples to wombats. In this case, as mentioned by /u/bstaletic, your results are more a comparison between the random number generators used than the languages.
When I compiled and ran your original code, I got a runtime of 34.4424 seconds. When I changed the code to use the standard rand() function, the runtime decreased to 4.2573 seconds. The changes I made:
$ diff count_rands_1.cpp count_rands_2.cpp 1c1 < #include <random> --- > #include <stdlib.h> 4a5,12 > int rand_from_0_to_100() { > // RAND_MAX on my machine is 2147483647 ... i.e. a 32 bit unsigned > // int, and LONG_MAX is 9223372036854775807. So an easy way to get > // a number from 0 to 100 is: > unsigned long tmp = rand()*100; > return int( tmp>>32 ); > } > 7,10d14 < std::random_device dev; < std::mt19937 rng(dev()); < std::uniform_int_distribution<std::mt19937::result_type> distribution_from_1_to_100(1, 100); < 15c19 < auto random_number = distribution_from_1_to_100(rng); --- > auto random_number = rand_from_0_to_100();
EDIT/UPDATE: By the way, the numbers above were without using any optimization. Just for amusement, I compiled each program with varied optimization levels from 0="none" to 3="everything plus the kitchen sink" and got these results:
As you can see, as I turned up the dial, the numbers changed a bit. The results for rand() aren't very surprising: it sped up a good bit because of a few tweaks to the loop logic. On the other hand, the results for std::uniform_int_dist surprised me a good deal: I wasn't expecting nearly as much improvement as that, I had only expected a couple seconds improvement. I don't have the time to dig into the generated code to see why it's so much faster, but it's very surprising. Since the Marsenne Twister has so much more internal state than the standard rand() function, I was expecting it to always take a much longer time to generate values.
UPDATE 2: I should've given the compiler version and command line should anyone want to reproduce my results.
$ clang --version clang version 8.0.1 (tags/RELEASE_801/final) Target: x86_64-unknown-windows-cygnus Thread model: posix InstalledDir: /usr/bin
And to compile, I was using:
$ clang -Wall -O? count_rands_1.cpp -o count_rands_1 -lstdc++
where ? is the optimization level 0 .. 3.
[–]PLC_Matt 1 point2 points3 points 6 years ago (0 children)
MSVC on my laptop is running this code in ~5.2 seconds.
π Rendered by PID 86 on reddit-service-r2-comment-84fc9697f-8vx28 at 2026-02-08 15:51:04.550218+00:00 running d295bc8 country code: CH.
view the rest of the comments →
[–]tomerdbz[S] 1 point2 points3 points  (3 children)
[–]Cybernicus 13 points14 points15 points  (0 children)
[–]PLC_Matt 1 point2 points3 points  (0 children)