C++ vs Java speed(Loops with Arithmetic) -
the following small programs compute sum of numbers 1 1 billion we're written in c++ , java closely write them. understanding c++ "faster" language, java version of code completes in ~.5 seconds vs ~3 seconds c++.
c++ (gcc compiler):
int main(){ long long x = 0; (long i=0;i<1000000001;i++){ x=x+i; } cout << x << endl; return 0; }
java:
public class main { public static void main(string[] args) { long x=0; (long i=0;i<1000000001;i++){ x=x+i; } system.out.println(x); } }
how 1 optimize c++ code fast java version? possible?
if compile optimizations, c++ version considerably faster.
java:
javac main.java $ time java main 500000000500000000 real 0m0.727s user 0m0.724s sys 0m0.004s
c++:
clang -o3 main.cpp -o cpp $ time ./cpp 500000000500000000 real 0m0.003s user 0m0.000s sys 0m0.000s
my clang version:
$ clang --version clang version 4.0.0-1ubuntu1 (tags/release_400/rc1) target: x86_64-pc-linux-gnu thread model: posix installeddir: /usr/bin
my java version:
$ javac -version javac 1.8.0_144
the reason optimization slow process; quicker compilation times if turn optimizations off. better development, defaults clang developers chose. java faster because more optimizations @ run-time. jvm bytecode not that different source-code compiled from!
Comments
Post a Comment