简体   繁体   English

Java 1.6与C ++的性能如何?

[英]Performance of Java 1.6 vs C++?

With Java 1.6 out can we say that performance of Java 1.6 is almost equivalent to C++ code or still there is lot to improve on performance front in Java compared to C++ ? 使用Java 1.6,我们可以说Java 1.6的性能几乎等同于C ++代码,或者与C ++相比,Java的性能方面还有很多需要改进的地方吗?

Thanks. 谢谢。

Debian likes to conduct benchmarks on this sort of thing . Debian喜欢对这类事情进行基准测试 In their case, it appears that Java is about half as fast and consumes 2-18 times as much memory as C++. 在他们的例子中,似乎Java的速度大约是C ++的一半,并且消耗的内存是C ++的2-18倍。

A well-written Java program is never going to be as fast as a well-written C or C++ program. 编写良好的Java程序永远不会像编写良好的C或C ++程序那样快。 The virtual machine is an irreducible overhead. 虚拟机是一种不可减少的开销。 However, most code is not well written. 但是,大多数代码编写得不好。

Java is a simpler language than C++, and offers a more forgiving environment for inexperienced programmers - so if your programmers are inexperienced (and cheap), then Java will probably perform 'better' than C++. Java是一种比C ++更简单的语言,为没有经验的程序员提供了一个更宽容的环境 - 所以如果你的程序员缺乏经验(并且便宜),那么Java可能比C ++表现得更好。

shared_ptr s offer a similarly forgiving environment in C++, so they are very tempting for inexperienced programmers, or those migrating from Java, But their performance overhead is as bad or worse than Java's garbage collection. shared_ptr在C ++中提供了一个类似的宽容环境,因此它们对于没有经验的程序员或从Java迁移的程序员来说非常诱人,但是他们的性能开销与Java的垃圾收集一样糟糕或差。 I've seen large C++ programs where every variable is a shared_ptr , and they perform terribly. 我见过大型C ++程序,其中每个变量都是一个shared_ptr ,它们执行得非常糟糕。

My opinion 我的意见

Personally, I think that large projects need to choose an 'easy' programming language for the bulk of their code, and a 'fast' one for sections that need optimising. 就个人而言,我认为大型项目需要为大部分代码选择“简单”的编程语言,而对于需要优化的部分则需要“快速”编程语言。 Java may be a good choice for the 'easy' language, especially since there is currently a plentiful supply of Java programmers - in the future, I think even easier languages such as Python will begin to take over. Java可能是“简单”语言的一个很好的选择,特别是因为目前有大量的Java程序员 - 在未来,我认为容易的语言如Python将开始接管。

C++ is a reasonable choice for a 'fast' language if you already know it, but I think it's over-complexity will eventually see it fall by the wayside, while C will continue to fulfill this role. 如果你已经知道C ++,那么C ++是一种“快速”语言的合理选择,但我认为它过于复杂,最终会看到它被淘汰,而C将继续履行这一职责。

I would expect that most of the time for most applications C++ will be faster than Java. 我希望大多数应用程序C ++的大部分时间都比Java快。

In some cases there will be some C++ which is slower than Java for a given task. 在某些情况下,某些C ++会比给定任务的Java慢。 This is pretty rare and always a result for poor implemntation or more commenly poor refactoring of an application. 这是非常罕见的,并且总是导致应用程序执行不佳或者应用程序的重构更加糟糕。

In the majority of cases the performance difference more than offset by the fexibility, ease of use, availability of libraries, and, portability that Java provides. 在大多数情况下,性能差异大大抵消了Java提供的灵活性,易用性,库的可用性以及可移植性。

In a very few cases performance is so critical that developing in Java would be a poor choice <opinion><flame off>in these cases plain C is usually a better choice than C++ </flame></opinion>. 在极少数情况下,性能至关重要,在Java中开发将是一个糟糕的选择<opinion> <flame off>在这些情况下,普通C通常是比C ++更好的选择</ flame> </ opinion>。

Currently the sweetspot in performance/ease of use/ease of development tradeoffs is C#. 目前,性能/易用性/易于开发权衡的甜点是C#。 Portability is a big issues here though. 虽然便携性是一个很大的问题。

I find that Java performs very well. 我发现Java表现得非常好。

However, why has no one ever fixed my biggest complaint? 但是,为什么没有人能解决我最大的抱怨?

Java uses FIVE TIMES as much memory as a C++ program doing the same job. Java使用FIVE TIMES作为执行相同工作的C ++程序的内存。 At least! 至少!

And once it's used, Java keeps it ! 一旦它被使用, Java就会保留它

Please, please, why won't anyone write a garbage collector for Java that uses minimum amounts of RAM? 请问,为什么没有人会为使用最少量RAM的Java编写垃圾收集器? It could compact the heap and returns the memory to the OS. 它可以压缩堆并将内存返回给操作系统。 Instead of ridiculous piles of -Xm* options, use the memory needed and then give it back ! 而不是荒谬的成堆-Xm *选项,使用所需的内存,然后将其还给

Actually I am sure some of the embedded system JVMs do this, but none of the desktop or server systems do. 实际上我确信一些嵌入式系统JVM可以做到这一点,但桌面或服务器系统都没有。

This memory piggishness makes Java applications all want to act as if they own the entire computer system, that no one ever wants to run more than one application and that RAM is free and infinitely upgradable. 这种内存贪婪使Java应用程序都希望表现得好像拥有整个计算机系统一样,没有人想要运行多个应用程序,并且RAM是免费且可无限升级的。

Therefore, no matter how great the performance, I would never write anything like a utility program in Java. 因此,无论性能如何出色,我都不会像Java中的实用程序那样编写任何内容。 Only gigantic server apps need apply. 只需要申请巨大的服务器应用程序。

What program are you developing? 你在开发什么课程?

Comparing C++ to Java speed is like comparing a screwdriver and a hammer, pointless. 将C ++与Java速度进行比较就像比较一把螺丝刀和一把锤子一样毫无意义。 In the world we live in, where both supercomputers and toasters need to be programmed, you need to focus on your particular requirements. 在我们生活的世界中,超级计算机和烤面包机都需要编程,您需要专注于您的特定要求。

I use C++ for hard realtime software running on embedded systems. 我将C ++用于在嵌入式系统上运行的硬实时软件。 I wouldn't dream of using the awfully broken Java for realtime spec for at least another 5 years, when it will hopefully be mature. 我不会梦想在至少再过5年的时间里使用非常破碎的Java实时规范,希望它有望成熟。 I would be equally loath to use C++ for a database, cloud accessing middleware app (actually I have no Idea what I just said, but I know Java is good for 'that sort of stuff') 我同样不愿意将C ++用于数据库,云访问中间件应用程序(实际上我不知道我刚刚说过什么,但我知道Java对于那种东西很有用')

Would you use a ferrari with no trunk space to move your belongings? 你会使用没有行李箱空间的法拉利来移动你的物品吗? Would you bring a minivan to a drag race? 你会带一辆小型货车去参加一场比赛吗?

People have to understand that just because they are programming languages, does not mean they are comparable in a meaningful way. 人们必须明白,仅仅因为它们是编程语言,并不意味着它们以有意义的方式具有可比性。

不会。除非你测量它,否则你可能不会说出来。

Performance is usually "good enough" for most purposes. 对于大多数用途,性能通常“足够好”。 The question is what you want to compare exactly, and if you have applied a profiler to find and fix the hotspots in your code. 问题是您想要准确比较什么,以及您是否已应用分析器来查找和修复代码中的热点。

JVM's based on Sun's code still pay a hefty startup-tax (I still wonder why they cannot snapshot that and restart from there) but Suns approach has been correctness first, speed second, and it's taken them 10 years to get up to par. 基于Sun的代码的JVM仍然支付了大量的启动税(我仍然想知道为什么他们不能快照并从那里重新启动)但是太阳队的方法首先是正确性,速度第二,并且花了他们10年才能达到标准。

So the answer is "It depends" :) 所以答案是“这取决于”:)

For most applications it is almost certainly possible to write a C++ program which performs considerably better than a program to achieve the same thing in java. 对于大多数应用程序来说,几乎可以肯定编写一个C ++程序,它比java程序在程序中表现要好得多。

However if the program isn't optimised for speed then java will likely be just as fast or faster because the compiler / JIT is able to make optimisations that a C++ environment can't. 但是,如果程序没有针对速度进行优化,那么java可能会同样快或者更快,因为编译器/ JIT能够优化C ++环境。

Basically if you are willing to spend considerable time understanding and coding for performance you can probably do a considerably better job in c++ eventually than you could in java but for the same amount of time and effort it is quite likely that java will "win". 基本上如果你愿意花费大量时间来理解和编写性能,你最终可能会比在java中做得更好,但是在相同的时间和精力下,java很可能会“赢”。

As usual though, algorithmic improvements tend to make as much if not more difference than the language. 像往常一样,算法改进往往会产生与语言不同的差别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM