3 ms·
We automatically generated all the results in this quiz from the programs on the site. None of the loops were optimized out, we ran basically a binary search to
by jvns 10y ago
We automatically generated all the results in this quiz from the programs on the site. None of the loops were optimized out, we ran basically a binary search to figure out the maximum number of iterations you could run in a second.
Results and compiler optimizations will of course vary across computers, but they were correct on my laptop on the day that we ran them (in Sept. 2015).
If you want to reproduce this on your computer and see how the results are different, you can clone https://github.com/kamalmarhubi/one-second https://github.com/kamalmarhubi/one-second and run `run_benchmarks.py`
- Johnny555 10y agoThe -O2 flag wasn't added to the benchmark script until Sept 20th in commit 4a31931, I suspect you ran at least sum.c without that flag since any modern gcc compiler should have optimized away that entire loop. I downloaded and ran the benchmarks and indeed the search for the 1 second runtime for sum.c never completed -- it never found an answer because the sum binary runs in constant time regardless of the iteration count. In fact, atoi() in sum.c quickly overflowed once the iteration count exceeded 2^31 (since atoi() returns a signed int). I removed the -O2 flag and then I got the same 550429840 iter count as you on my laptop. The benchmark script isn't really doing a binary search, it's doing a linear search since it's increasing the iteration count by 10% with each loop.
- alkonaut 10y agoNo, look again. it's constant time. Yes, "optimizations will vary" - but any C compiler with reasonable settings should just ignore the loop. Otherwise you are running so un-optimized that there is no point in talking about the runtmie of the program anyway. You should change it to return the sum.