1.5 billion iterations in 10 seconds! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

1.5 billion iterations in 10 seconds!

Around 12 hours ago, Agus Mei posted a puzzle (https://www.sololearn.com/discuss/299146/?ref=app). To solve it, I wrote a C++ code. It took around 100 seconds to complete the work. Then, I converted it into a Java program (https://code.sololearn.com/c10BAhXfFGbP/?ref=app) and executed it, and am surprised to see that the execution completed in less than 10 seconds! There are around 1.5 billion iterations in it! I don't know much about C++ but is it really that slow? Or, is there any other reason for this?

7th Apr 2017, 3:14 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
6 Answers
+ 9
Sir, I can bet that It wouldn't take more than 6 s on a Win 7 PC with Code::Blocks ... Perhaps the C++ compilation server is overloaded... Please try it on an offline compiler!
7th Apr 2017, 7:22 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 7
if on the Code Playground depends on how "busy" is the server.
7th Apr 2017, 3:44 AM
seamiki - avatar
+ 4
would you guys say c++ or java is faster? i always heard it was c++
7th Apr 2017, 3:46 AM
+ 4
Just executed the C++ version in Turbo C++ compiler. It took around 350 seconds!!!
7th Apr 2017, 4:12 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 3
@edward c++ is at least as fast as java. nowadays it's all about optimisation. but not your optimisation but of the compiler. so the real question is which compiler produces faster executable code? the answer: both do well. you will not see any difference by a magnitude anymore. there are edge cases where c++ outperforms java though.
7th Apr 2017, 7:49 AM
Petja Boigk
Petja Boigk - avatar
+ 3
@seamiki : Only 1 result will be printed in the code playground before "Time limit exceeded". I executed C++ version offline and Java version in tutorials point online compiler.
7th Apr 2017, 12:58 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar