西西河

主题:【原创】Java和.NET哪个运行的更快? -- Highway

共:💬24 🌺19 新:
全看树展主题 · 分页首页 上页
/ 2
下页 末页
家园 就是,我工作中就碰到不少用Java开发,运行一段时间后

发现有memory leak的项目。

虽然Java不给程序员直接操作指针,但很多其他资源不释放,一样有内存泄露。

家园 看来睡虫女侠是主管一级,牛!
家园 【补充】.NET设计师谈Hotspot和.NET的JIT

Anders Hejlsberg是我崇敬的一位IT大师,他是微软十虎将之一(Ten distinguished engineer),他是丹麦人,一个人搞出了Pascal,后来又领导开发了Delphi,VJ++。现在他领导着微软的C#团队,并且是.NET核心创始人之一。我听过他的不少讲话,非常诚恳,坦率和幽默。兄弟我很是佩服!

点看全图

外链图片需谨慎,可能会被源头改

下面是他在接受采访时对话的节选。建议看看。

Bill Venners: One difference between Java bytecodes and IL [Intermediate Language] is that Java bytecodes have type information embedded in the instructions, and IL does not. For example, Java has several add instructions: iadd adds two ints, ladd adds two longs, fadd adds two floats, and and dadd adds two doubles. IL has add to add two numbers, add.ovf to add two numbers and trap signed overflow, and add.ovf.un to add two numbers and trap unsigned overflow. All of these instructions pop two values off the top of the stack, add them, and push the result back. But in the case of Java, the instruction indicates the type of the operands. A fadd means two floats are sitting on the top of the stack. A ladd means there two longs are sitting on the top of the stack. By contrast, the CLR's [Common Language Runtime] add instructions are polymorphic, they add the two values on the top of the stack, whatever their type, although the trap overflow versions differentiate between signed and unsigned. Basically, the engine running IL code must keep track of the types of the values on the stack, so when it encounters an add, it knows which kind of addition to perform.

I read that Microsoft decided that IL will always be compiled, never interpreted. How does encoding type information in instructions help interpreters run more efficiently?

Anders Hejlsberg: If an interpreter can just blindly do what the instructions say without needing to track what's at the top of the stack, it can go faster. When it sees an iadd, for example, the interpreter doesn't first have to figure out which kind of add it is, it knows it's an integer add. Assuming someone has already verified that the stack looks correct, it's safe to cut some time there, and you care about that for an interpreter. In our case, though, we never intended to target an interpreted scenario with the CLR. We intended to always JIT [Just-in-time compile], and for the purposes of the JIT, we needed to track the type information anyway. Since we already have the type information, it doesn't actually buy us anything to put it in the instructions.

Bill Venners: Many modern JVMs [Java virtual machines] do adaptive optimization, where they start by interpreting bytecodes. They profile the app as it runs to find the 10% to 20% of the code that is executed 80% to 90% of the time, then they compile that to native. They don't necessarily just-in-time compile those bytecodes, though. A method's bytecodes can still be executed by the interpreter as they are being compiled to native and optimized in the background. When native code is ready, it can replace the bytecodes. By not targeting an interpreted scenario, have you completely ruled out that approach to execution in a CLR?

Anders Hejlsberg: No, we haven't completely ruled that out. We can still interpret. We're just not optimized for interpreting. We're not optimized for writing that highest performance interpreter that will only ever interpret. I don't think anyone does that anymore. For a set top box 10 years ago, that might have been interesting. But it's no longer interesting. JIT technologies have gotten to the point where you can have multiple possible JIT strategies. You can even imagine using a fast JIT that just rips quickly, and then when we discover that we're executing a particular method all the time, using another JIT that spends a little more time and does a better job of optimizing. There's so much more you can do JIT-wise.

Bill Venners: When I asked you earlier (In Part IV) about why non-virtual methods are the default in C#, one of your reasons was performance. You said:

We can observe that as people write code in Java, they forget to mark their methods final. Therefore, those methods are virtual. Because they're virtual, they don't perform as well. There's just performance overhead associated with being a virtual method.

Another thing that happens in the adaptive optimizing JVMs is they'll inline virtual method invocations, because a lot of times only one or two implementations are actually being used.

Anders Hejlsberg: They can never inline a virtual method invocation.

Bill Venners: My understanding is that these JVM's first check if the type of the object on which a virtual method call is about to be made is the same as the one or two they expect, and if so, they can just plow on ahead through the inlined code.

Anders Hejlsberg: Oh, yes. You can optimize for the case you saw last time and check whether it is the same as the last one, and then you just jump straight there. But there's always some overhead, though you can bring the overhead down to fairly minimum.

家园 Garbage collector的效率

与程序类型/风格有关.最早使用Garbage collector

的是Functional languages. 这类语言的程序有大量

short-lived small objects, Garbage collector很

有兢争力.Mark-and-sweep虽然可能比malloc/free

慢一点,但是可以有效解决fragmentation.Copying

collection(尤其是generational GC)则进一步在速度上

也有一争.

另外,比较效率也要从整体上比.举一个例子.我正在

写的一个程序有一个producer和多个independent consumer.

每个object都要接收多个consumer的处理,也可能被一个

consumer传给另一个consumer,还可能需要存下来.如果没有GC,要么用

single-thread固定流程,要么create one copy for each consumer,

要么每个consumer都要执行reference-counting routine以

决定是否free一个object.每个选择都对系统的结构/效率有不

好的影响.

我觉得GC被贬的原因主要有:

1. GC 不适合memory-scarce environment.

2. GC memory footprint 较大影响cache效率. (不过对绝大多数

人, 包括C programmers, 比cache效率大的问题多了去了.)

3. 早期GC implementation对Stack处理不好造成retention.

元宝推荐:Highway,

本帖一共被 1 帖 引用 (帖内工具实现)
家园 现在计算机的处理能力的增长速度已经大大地减慢了。

如果不考虑资源的优化问题,迟早会出问题的。

家园 这个怎么没推荐?

好东西不要不好意思多给人看嘛.

家园 那你赶紧帮着推荐
家园 其实C++中使用Smart pointer

可以大部分缓解没有GC的问题,减少程序操作指针的错误。

当然GC是一个非常好的feature,如果resource不是critical的话。

家园 当版主的唯一不便之处是不好意思给自己的好帖子加精,

这实际上会造成一些好帖子不能方便地被会员找到。

应该有一个机制,找一些人,他们专门给版主且也只能给版主加精。

全看树展主题 · 分页首页 上页
/ 2
下页 末页


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河