什么是faster- Java或C#(或好老C)?faster、Java

2023-09-03 08:43:56 作者:做個没心没肺菂尐女亽

我目前决定一个平台的基础上科学计算的产品,我决定无论是C#,Java或纯C采用英特尔编译器的酷睿2四核CPU的。这主要是整数运算。

I'm currently deciding on a platform to build a scientific computational product on, and am deciding on either C#, Java, or plain C with Intel compiler on Core2 Quad CPU's. It's mostly integer arithmetic.

我的基准,到目前为止表明Java和C约看齐,与对方和.NET / C#径约5% - 但我的一些同事都声称.NET使用正确的优化将击败这两个给予足够的时间为JIT做其工作。

My benchmarks so far show Java and C are about on par with each other, and .NET/C# trails by about 5%- however a number of my coworkers are claiming that .NET with the right optimizations will beat both of these given enough time for the JIT to do its work.

我总是假设JIT会做它的工作应用程序的几分钟内开始(在我的情况大概几秒钟,因为它主要是密集的循环),所以我不知道是否应该相信他们

I always assume that the JIT would have done it's job within a few minutes of the app starting (Probably a few seconds in my case, as it's mostly tight loops), so I'm not sure whether to believe them

任何人都可以摆脱对局势的光?将.NET击败的Java? (还是我最好只是用C贴在这一点?)。

Can anyone shed any light on the situation? Would .NET beat Java? (Or am I best just sticking with C at this point?).

在code是非常多线程和数据集有几个TB的。

The code is highly multithreaded and data sets are several terabytes in size.

哈斯克尔/二郎等不在这种情况下的选择是有显著量的现有传统的C code将被移植到新的系统,并移植C到的Java / C#是不是哈斯克尔简单得多或二郎。 (当然,除非这些提供一个显著加速)。

Haskell/Erlang etc are not options in this case as there is a significant quantity of existing legacy C code that will be ported to the new system, and porting C to Java/C# is a lot simpler than to Haskell or Erlang. (Unless of course these provide a significant speedup).

编辑:我们正在考虑转移到C#或Java,因为它们可能在理论上会更快。我们可以剃掉了处理时间,每%的节省了我们的数万美元每年。在这一点上,我们只是试图评估是否C,Java或C#会更快。

We are considering moving to C# or Java because they may, in theory, be faster. Every percent we can shave off our processing time saves us tens of thousands of dollars per year. At this point we are just trying to evaluate whether C, Java, or c# would be faster.

推荐答案

在问题的信息的关键部分是这样的:

The key piece of information in the question is this:

每%的,我们可以剃掉我们   处理时间为我们节省了数万   数千美元,每年

Every percent we can shave off our processing time saves us tens of thousands of dollars per year

所以,你需要考虑的这将花费多少的为关闭刮胡子每个百分点。如果优化工作花费美元的每年几万,那么它是不值得做的事情。您可以通过发射程序员做出更大的节约。

So you need to consider how much it will cost to shave each percent off. If that optimization effort costs tens of thousands of dollars per year, then it isn't worth doing. You could make a bigger saving by firing a programmer.

使用正确的技能(这是今天罕见,因此更贵),你可以手工工艺汇编,以获得最快的code。略少稀有而昂贵的技能,你可以用一些非常难看C code执行几乎一样好。等等。将更多的性能,你挤它,它越会花费你的开发工作,并且会有收益递减,直到永远更大的努力。如果从这个利润停留在美元的每年几万,那么随之而来的一个点,它已不再是值得的。事实上,我敢大胆地你已经在这一点猜测,因为数十万美元,每年是在一个工资的范围,大概不够买所需的技能手工优化一个复杂的程序。

With the right skills (which today are rarer and therefore more expensive) you can hand-craft assembler to get the fastest possible code. With slightly less rare (and expensive) skills, you can do almost as well with some really ugly-looking C code. And so on. The more performance you squeeze out of it, the more it will cost you in development effort, and there will be diminishing returns for ever greater effort. If the profit from this stays at "tens of thousands of dollars per year" then there will come a point where it is no longer worth the effort. In fact I would hazard a guess you're already at that point because "tens of thousands of dollars per year" is in the range of one salary, and probably not enough to buy the skills required to hand-optimize a complex program.

我猜想,如果你已经写在C code,改写这一切的直接翻译其他语言的努力将是90%无用功。它很可能会进行简单的比较慢,因为你不会被利用的平台的能力,而是工作对他们来说,如尝试使用Java,仿佛它是℃。

I would guess that if you have code already written in C, the effort of rewriting it all as a direct translation in another language will be 90% wasted effort. It will very likely perform slower simply because you won't be taking advantage of the capabilities of the platform, but instead working against them, e.g. trying to use Java as if it was C.

在现有的code也,将有零件做出的运行时间(他们经常跑)作出重要贡献,而且是完全不相关的(它们运行很少)等部位。所以,如果你有一些想法,对于加快程序,就没有经济意义的浪费时间把它应用到不影响运行时程序的部分。

Also within your existing code, there will be parts that make a crucial contribution to the running time (they run frequently), and other parts that are totally irrelevant (they run rarely). So if you have some idea for speeding up the program, there is no economic sense in wasting time applying it to the parts of the program that don't affect the running time.

因此​​,使用分析器找到热点,并看到时间被浪费了在现有的code。

So use a profiler to find the hot spots, and see where time is being wasted in the existing code.

更新时,我注意到参考code是多线程

在这种情况下,如果你集中你的精力,消除瓶颈,使你的程序可以通过大量的核心很好地扩展,然后它会自动获得更快的速度,每年的速率将大大超过任何其他的优化​​可以进行。明年这个时候,四核将是台式机的标准。一年之后,8个内核将会越来越便宜(我买了一个在一年前的几千元),我会predict一个3​​2核心机将花费不到一个开发到那个时候。

In that case, if you focus your effort on removing bottlenecks so that your program can scale well over a large number of cores, then it will automatically get faster every year at a rate that will dwarf any other optimization you can make. This time next year, quad cores will be standard on desktops. The year after that, 8 cores will be getting cheaper (I bought one over a year ago for a few thousand dollars), and I would predict that a 32 core machine will cost less than a developer by that time.