C++11(native code) vs Java(bytecode) [closed]

2019-07-11 13:49发布

Lately I've been thinking about the difference about native and bytecode. I did some research and all information I've read was about C++ code compiled into native code vs Java code compiled into bytecode(runs on JVM which takes advantage of multi-core processors). The conclusion was that applications written in Java run faster.

Does C++11 change this? Does applications written in C++11(since adds threads) run faster than Java ones?

3条回答
三岁会撩人
2楼-- · 2019-07-11 14:24

I would just say All Your Java Bases Are Belong To C++.. The JVM itself is written in C/C++. C/C++ run at native speeds on the bare-metal of the machine, while bytecodes are interpreted by a C/C++ code(that's running on top of the metal). One byte-code instruction could translate to about 5-10 asm instructions(or more). Hence speed of execution of C/C++ is considered faster than Java's. Ofcourse, if Java's runtime were thrown onto the metal and we had bytecode interpreted at machine speed, then it would be a fair comparison.

That said, see an example in the book called "Programming Pearls" where the author runs an interpreted BASIC program on a Radioshack personal computer, which with sufficient optimizations, runs faster than it does on a super computer. Which means, speed of execution of your program depends on your algorithms and coding/optimization practices.

查看更多
乱世女痞
3楼-- · 2019-07-11 14:33

Neither C++ nor Java automatically split your program into multiple threads. The closest you can get to automatic parallelization in modern languages is to use parallel collections. There are libraries to do that in C++ but there is better support for that kind of stuff in more functional languages e.g. Haskell, Scala, Clojure. Another way to get automatic parallelization is to use an actor library and write your entire program with actors. Erlang was the first language with full support for that but the Akka framework for Scala/Java is also very good.

查看更多
叼着烟拽天下
4楼-- · 2019-07-11 14:38

The conclusion was that applications written in Java run faster.

That's a big leap to come to. There are so many factors which contribute to the performance of a system that it's very hard to say one approach will always be better or even faster than another.

C++ has always been able to use threads, it just didn't have as much detail on how they could be used. I don't believe C++11 is about performance but standardising things like memory models.

IMHO Given any amount of time and expert developers, a C++ program will always be faster than a Java program. However, given a limited amount of time and developers of mixed ability you are more likely to get something working and performing well in Java. Your mileage will vary. ;)


Making my answer clearer...

Does C++11 change this?

No. I don't agree it is the situation nor does it change it.

Does applications written in C++11(since adds threads) run faster than Java ones

Yes, but not always, Just like earlier versions.

查看更多
登录 后发表回答