C++ vs. Java 1.6 - A fair Benchmark <!---->
For one of my small private projects, I wanted to know if I could implement the program in Java instead of C++. The only critical feature for this program was speed, so I needed to know how fast Java is today using the latest VM, compared to C++ using a modern optimizing compiler.
There are lots of tests and benchmarks available on the web, but usually I don't trust any statistics I did not fake myself. So I did my own benchmark with 3 simple real-world test cases and one additional academic test. The tests I found on the web have been mostly usesless, especially if you are programming games. These are the ones I used:
- Sorting a huge array (5 mio doubles) using Heapsort, testing usual, random memory access quite well.
- Calling multiple nested loops (4), each one 280 times, computing a number in the inner loop based on the loop counters.
- Copying an array (8 MB of doubles) into another one, multiple times (100 times).
- Calculating 44 fibonacci numbers, testing recursive calls. Totally useless test for real world programs, but because every benchmark seems to use it, it was interesting to include it as well.
I tried to be very fair because I really needed realistic results to base my decision on. You can get the source code of my tests here to verify or review my tests here:
benchmark.zip. If you like, extend it or use it for whatever you like.
The results
To make it short: As I expected C++ outperforms Java in every single test. But interestingly Java is getting closer now, here are my average results:
|
Java |
C++ |
Fibonacci |
10767 ms |
7468 ms |
Heapsort |
5454 ms |
4344 ms |
NestedLoops |
8022 ms |
3453 ms |
CopyData |
1204 ms |
1125 ms |
Details and Findings
Knowing a bit about the C++ and Java optimizers, I was also able to revert the results completely. For example although using equivalent Java and C++ code, I was able to make the C++ version take 16 seconds for the nested loops-test while Java only needed 1 second by adding some virtual method calls which I knew Java could optimize. I also was able to make the C++ version run in only 78 ms while the equivalent Java version needed even 10 seconds by only using static loops - the C++ optimizer then would not even loop anything and just add the values together. This is partially what some of the wrong benchmarks out there are doing - even if not intentional. If you take a look at the sourcecode of my benchmark, you'll also see that I only measured the actual calculations, I was not interested in the start up times at all. This would have been very unfair because java needs some more time to start up, and it doesn't matter at all if an end user program (especially game) needs some extra seconds in the beginning. That's also the reason I used the client and not the server java VM: No end user is using the server VM at all. Another detail was interesting: Java was running out of heap memory very fast. I even had to reduce the sizes of the arrays in the tests, Java wasn't even able to allocate two arrays of several million doubles. Of course this wasn't a problem on the C++ side at all.
My conclusion: Java is not as fast as C++ although asserted by several people. Simply as that. But with today's incredible hardware, nothing will prevent you from writing simple games with it, unless you are not trying to create a fast Software Rasterizer or similar.
18 comments, already:
interesting stuff. java has gotten quite fast as it seems. at least in some areas…
bakkdoor (link) - 29 01 07 - 22:13
Many thanks for that.
Funnily enough I really can use that right now :)
I wouldn’t have thought using virtual mehtods is such a slow-downer… still I think the Java programs are still slow in comparement to C++. And I hate to live without default parameters or (at a smaller scale) without operator overloading.
I just grew up with that :)
Gauntlet - 29 01 07 - 22:21
Well nice figures. Thanks for that!
But I have to add that though being very accurate and more fair than other tests I saw already – this is only half the truth again. I would admit that it is correct to say java is slower than c++ when it comes to primitve datatypes and optimizing nested loops but there are real objects also where the numbers may change somewhat. In fact it will depend upon the tasks.
By the way: the server vm is used often on servers’s – that is what it is optimzed for. The client VM is faster for running on machines using swing ui. Would be interesting to do some benchmerks to see what happens if the server vm is to be tested with jirr – the java binding of irrlicht.
To make it short: the server vm is ahead of the client vm by some frames (1-3%) in the tech demo (running java 6) when using default values – though it is possible to tune the client vm to reach even higher numbers.
I just was asthonished to see a decrease within the fibonacci test duration by roughly 20% by using the server vm whereas the nested loops were signifcant slower.
Also please note that java is limiting its maximum memory usage to 128MB by default: -Xmx256m (or any higher number) will increase that … There is also a switch for the initial heapsize: -Xms. http://performance.netbeans.org/howto/jv.. lists some more paramters though with every new java version there seem to be more flags – please ask sun for any hidden ones
PS: Do not blame java for being slow on the client when it is the programmer’s fault. The swing ui is somewhat critical to running cpu intensive tasks without launching such a task in a separate thread. That will stop rendering of the gui until the main task returns. And guess what several coders are hacking in …
java protagonist - 29 01 07 - 23:55
I think that Java can be closer to C++ in big real world examples, but of course it is difficult to have more than an impression about that, as almost no big C++ app has been translated to Java. May be it is because it is very easier for an average programmer to optimize a Java program than a C++ one (in C++, you have also to take care of memory leaks, pointers, etc… this can be a lot of work).
The only two examples that I know of that have been implemented both in C++ (or C) and Java are http://www.bytonic.de/html/jake2.html (Jake2) ,a
Hervé (
<script type="text/javascript">
<!---->
</script>
email) (link) - 29 01 07 - 23:59
(sorry, some mistyping).
The only two examples that I know of that have been implemented both in C++ (or C) and Java are http://www.bytonic.de/html/jake2.html (Jake2), a clone of Quake2 in Java, and http://cmusphinx.sourceforge.net/sphinx4.. (Sphynx), a speech recognizer first coded in C, then in Java. Jake2 performance in Java 5.0 are identical to the original C versions (in some configurations), and Sphynx4 (in Java) performs better than the C version.
But of course, to be fair, I have to say that Jake2 still binds to a low-level C OpenGL wrapper, and the algorithms in the Java version have surely been improved for performance…
Hervé (
<script type="text/javascript">
<!---->
</script>
email) (link) - 30 01 07 - 00:05
@Hervé: C++ programmers who really have to care about memory leaks and memory management in general have completely messed up (and I’m not talking about using goehm-gc et al).
ak (
<script type="text/javascript">
<!---->
</script>
email) (link) - 30 01 07 - 00:23
next up, c++ vs. C#, the ultimate benchmark throwdown!!
that’s one i wanna see
buhatkj - 30 01 07 - 00:50
Yes me too Niko I wonder how C# would perform against C++ and Java… After all its only fair that someone did this…
leo (
<script type="text/javascript">
<!---->
</script>
email) (link) - 30 01 07 - 07:51
And it would be interesting to see memory consumption too :P
Raedwulf (
<script type="text/javascript">
<!---->
</script>
email) - 30 01 07 - 09:35
I’ve never heard of anyone claiming Java is faster than C++ – that would be pretty much impossible since there is still a layer of interpretation and garbage collection going on, no matter how well optimised the VM is.
Most of the time when I worked extensively in (server-side) Java I had to defend that Java was fast enough for the types if application that should be written in it. Most people judging it did it with either very old VMs or on application tests which were totally unsuited for the sort of application you would typically build in Java. It’s like saying scripting languages are slower than C++ when running tight loops – well, duh. Too many developers look at the minutae without seeing the big picture – that it’s worth trading a known amount of performance in certain cases for benefits in other areas.
Steve (
<script type="text/javascript">
<!---->
</script>
email) (link) - 30 01 07 - 12:10
here is one claiming java is faster: http://kano.net/javabench/index
erik - 30 01 07 - 12:36
This prooved Java is really fast… for an interpreted language. Of course it can not be nearly as fast as C++. But a C++ vs C# would be cool, yes :)
jasper - 30 01 07 - 12:59
It would be interesting to hear about compiler version and compilation flags. Moreover you should Java grant some startup time, so better inline several calls to the test function and average over the inner call times. Also the latest gccs still optimize the loops away with many optimization settings.
hybrid - 30 01 07 - 14:34
I used the VisualStudio 2005 compiler with the standard release mode settings. Hm, another test with different compilers/settings would be interesting as well, right :)
But I don’t think it’s completely impossible that java programs are faster than c++: Java has the advantage that it can inline virtual functions during runtime and also has the GC, which can compact the heap resulting in better caching where C++ is forced to work with a fragmented heap. Lots of applications can profit from this resulting in possible faster speed than C++.
niko - 30 01 07 - 18:13
Java Vs C# which one is better for making a 3D game.
leo (
<script type="text/javascript">
<!---->
</script>
email) (link) - 31 01 07 - 16:40
ok..c++ out performs java…i would like to know how fast c# is,’cos i am working on a project with c#..
tunde (
<script type="text/javascript">
<!---->
</script>
email) (link) - 02 02 07 - 12:48
You never mentioned which C++ compiler you’re using. That could make a big difference. Using g++ 3.4.4 on Windows under Cygwin with -O3 option, I get these times:
fib: 17341
heap: 4770
nest: 4628
copy: 1932
Here is the code to use the non-MS-specific gettimeofday() call:
struct timeval tv1;
struct timeval tv2;
cout
Andy Tripp (
<script type="text/javascript">
<!---->
</script>
email) - 07 02 07 - 03:57
I see many people are interested in c# results… so was I. Here are my results with java (1.6), c# (vs2005sp1, release), c++ (vs2005sp1, release) on Athlon 64 3200+ Windows XP machine:
java: fib: 9531, heap: 3640, loops: 12688, copy: 1500
c# : fib: 12671, heap: 3765, loops: 9406, copy: 1453
c++ : fib: 8750, heap: 3594, loops: 6328, copy: 1500
c# port is pretty straightforward from java… One more thing. None of this languages is interpereted… c# and java use just-in-time compiler, while c++ uses “classic” one. All results are surprisingly close to each other and all languages should now be usable for most purposes. (Someone could try compiling java source to native (GCJ)?)
bd (
<script type="text/javascript">
<!---->
</script>
email) - 12 02 07 - 17:34
分享到:
相关推荐
资源分类:Python库 所属语言:Python 资源全名:pytest_benchmark-3.1.1-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
领域Java基准测试 //07-10 00:34:33.765 12003-12003/io.realm.examples.intro E/IntroExampleActivity﹕ Create Object Time#: 149//07-10 00:34:46.955 12877-12877/io.realm.examples.intro E/...
./src/benchmark-reporter.js 一个非常简单的报告器,将信息记录到history对象并打印信息到console 。 这在浏览器和带有的cli中均适用。 这个基本的报告器捕获从Benchmark.Suite发出的事件,并将其发送到处理程序...
java-object-mapper-benchmark, Java对象到对象映射框架的JMH基准 Object-to-object映射框架微模块多层应用程序通常需要在不同对象模型之间进行映射( 比如 。 ipqos和实体。写这样的锅炉板映射代码是一个令人烦恼和...
根据给定的文件信息,我们可以总结出以下关于2016年5月移动游戏Benchmark的重要知识点: ### 一、付费率分析 - **整体趋势**:2016年5月,无论是Android还是iOS平台上的移动游戏,其付费率都有所回升。 - **分类...
此为ASCE的第三代基准模型代码,可供土木工程领域学者使用。
jedis.rar java为客户端 源码和jar包 commons-pool.jar jedis 的依赖包 RedisSimpleTest.rar 为示例工程 源码 和相应的jar都在里面 参考地址:**一定要看** ...
http-benchmark-tornado 解决方案web测试工具压力不足,压力不均匀,统计输出不完备,扩展不灵活等所有缺陷。测试工具参数配置灵活,可满足一般性能测试,延迟测试,最大连接数测试,防爆测试,压力测试,持久稳定性...
AS SSD Benchmark 1.6是一款SSD固态硬盘专用的测试工具,它可以测试SSD的连续读写、4K、4K-6K随机、访问量及响应时间的表现,并给出一个综合的评分。是针对SSD的一款专门测试工具,能够进行持续读写和随机读写...
lucene-benchmark-3.3.0.jar lucene-core-3.3.0.jar lucene-demo-3.3.0.jar lucene-grouping-3.3.0.jar lucene-highlighter-3.3.0.jar lucene-icu-3.3.0.jar lucene-instantiated-3.3.0.jar lucene-memory-3.3.0.jar...
2. **redis-benchmark.exe**:这是一个性能测试工具,可以用来测量Redis服务器的读写速度。 3. **redis-cli.pdb**:客户端命令行界面的调试信息文件。 4. **redis-check-aof.pdb**:用于检查Append Only File(AOF)...
redis-benchmark.exe:性能测试,用以模拟同时由N个客户端发送M个 SETs/GETs 查询 (类似于 Apache 的ab 工具). 启动测试: redis-server.exe redis.conf 连接测试: redis-cli.exe -h localhost -p 6379
js-framework-benchmark, 几种流行javascript框架性能的比较 js-framework-benchmark这是几个javascript框架的简单基准。 基准测试创建一个带有随机条目的大型 table,并测量各种操作的时间,包括渲染持续时间。 ...
python库。 资源全名:gnss_benchmark-2.0.0.post10-py3-none-any.whl
AS SSD Benchmark是一款专业的固态硬盘性能测试工具,由德国InnoDisk公司开发,主要用于评估SSD(固态硬盘)和HDD(机械硬盘)的读写速度、IOPS(每秒输入/输出操作次数)等关键性能指标。该工具1.6.4237.30508版本...
KPI_Benchmark_TD-LTE_RL55_NetworkLevel_day_V1.6-ex
使用`redis-cli.exe`与服务器通信,通过`redis-benchmark.exe`测试性能,同时利用`redis-check-dump.exe`来验证数据的完整性。当遇到问题时,可以参考`Windows Service Documentation.docx`和`Redis on Windows.docx...
D:\src\ujson-1.18\tests>python benchmark2.py ujson decode : 404.85833 calls/sec simplejson decode : 33.34445 calls/sec cjson decode : 246.91360 calls/sec ujson encode : 460.82948 calls/sec simplejson ...
AS SSD Benchmark是一款专业的固态硬盘性能测试工具,由德国InnoDisk公司开发。这款软件的主要目的是为用户提供一个简便的方法来评估他们的SSD(固态硬盘)或HDD(机械硬盘)的读写速度,以及I/O操作性能。1.6.4194...