前两天水群,和别人扯到C++和Java的性能对比,感觉现在好多人已经不再说java慢了,甚至好多人认为java性能已经和C++差不多了。但是正好我司有个模块在尝试java转C++,这就引来大家的一片质疑,后来我在网上找到这样一份数据,大多数情况下C++无论在运行速度还是内存占用,比Java有更大的优势。
最终,我们讨论的结论是,java这些年来在性能上提升了非常多,甚至平均性能已经与C++不相伯仲了,但是在某些对性能要求极高的情况下还是不及C++。也就是说C++的性能天花板要比Java高一些。但反过来,Java的开发效率和其生态也是C++没法比。
整理后的数据如下,原文https://benchmarksgame-team.pages.debian.net/benchmarksgame/faster/java-gpp.html
reverse-complement
source
secs
mem
gz
cpu
cpu load
Java
3.32
609,712
2183
7.54
64% 44% 44% 76%
C++ g++
2.93
980,716
2280
4.52
51% 23% 33% 51%
pidigits
source
secs
mem
gz
cpu
cpu load
Java
3.13
37,364
938
3.36
99% 2% 3% 4%
C++ g++
1.89
4,380
513
1.88
2% 99% 4% 2%
fasta
source
secs
mem
gz
cpu
cpu load
Java
2.32
42,212
2473
6.30
65% 85% 47% 76%
C++ g++
1.33
1,740
2711
4.29
81% 81% 81% 82%
fannkuch-redux
source
secs
mem
gz
cpu
cpu load
Java
17.98
31,180
1282
70.57
97% 99% 97% 99%
C++ g++
10.12
1,852
980
39.95
100% 100% 96% 100%
spectral-norm
source
secs
mem
gz
cpu
cpu load
Java
4.26
33,572
950
16.39
96% 96% 98% 95%
C++ g++
1.98
1,164
1044
7.87
99% 99% 99% 100%
binary-trees
source
secs
mem
gz
cpu
cpu load
Java
8.39
960,056
835
27.39
81% 85% 74% 90%
C++ g++
3.68
119,052
809
11.93
98% 76% 77% 78%
n-body
source
secs
mem
gz
cpu
cpu load
Java
22.00
32,272
1489
22.07
1% 0% 0% 100%
C++ g++
9.42
1,704
1763
9.41
2% 100% 1% 0%
k-nucleotide
source
secs
mem
gz
cpu
cpu load
Java
8.74
470,116
1812
27.12
71% 73% 93% 74%
C++ g++
3.73
156,080
1624
11.60
75% 70% 72% 96%
mandelbrot
source
secs
mem
gz
cpu
cpu load
Java
6.96
76,316
796
27.06
97% 97% 97% 97%
C++ g++
1.51
25,640
1791
5.99
100% 100% 99% 99%
regex-redux
source
secs
mem
gz
cpu
cpu load
Java
10.50
571,736
929
31.63
71% 73% 66% 92%
C++ g++
1.83
203,520
1315
4.41
50% 45% 100% 49%
Java
openjdk 11 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
C++ g++
g++ (Ubuntu 8.2.0-7ubuntu1) 8.2.0
原文中还有Java vs C, Java vs C#,Java vs Substrate VM的对比,java和C就差更多了,但是和C#旗鼓相当。