C++ 性能与 Java/C# [英] C++ performance vs. Java/C#

查看:21
本文介绍了C++ 性能与 Java/C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,C/C++ 生成在特定机器架构上运行的本机代码.相反,Java 和 C# 等语言运行在虚拟机之上,该虚拟机抽象了本机架构.从逻辑上讲,由于这个中间步骤,Java 或 C# 似乎不可能与 C++ 的速度匹敌,但是我听说最新的编译器(热点")可以达到甚至超过这个速度.

My understanding is that C/C++ produces native code to run on a particular machine architecture. Conversely, languages like Java and C# run on top of a virtual machine which abstracts away the native architecture. Logically it would seem impossible for Java or C# to match the speed of C++ because of this intermediate step, however I've been told that the latest compilers ("hot spot") can attain this speed or even exceed it.

也许这更像是一个编译器问题而不是一个语言问题,但是谁能用简单的英语解释一下这些虚拟机语言中的一种如何比母语执行得更好?

Perhaps this is more of a compiler question than a language question, but can anyone explain in plain English how it is possible for one of these virtual machine languages to perform better than a native language?

推荐答案

通常,C# 和 Java 可以一样快或更快,因为 JIT 编译器——一个在你的 IL 第一次执行时编译的编译器——可以使C++ 编译程序无法进行的优化,因为它可以查询机器.可以判断机器是Intel还是AMD;Pentium 4、Core Solo 或 Core Duo;或者是否支持SSE4等

Generally, C# and Java can be just as fast or faster because the JIT compiler -- a compiler that compiles your IL the first time it's executed -- can make optimizations that a C++ compiled program cannot because it can query the machine. It can determine if the machine is Intel or AMD; Pentium 4, Core Solo, or Core Duo; or if supports SSE4, etc.

一个 C++ 程序必须事先编译,通常使用混合优化,以便它在所有机器上运行良好,但没有像单个配置(即处理器、指令集、其他硬件)那样优化.

A C++ program has to be compiled beforehand usually with mixed optimizations so that it runs decently well on all machines, but is not optimized as much as it could be for a single configuration (i.e. processor, instruction set, other hardware).

此外,某些语言功能允许 C# 和 Java 中的编译器对您的代码进行假设,从而允许它优化某些部分,而这些部分对于 C/C++ 编译器来说是不安全的.当您可以访问指针时,有很多优化并不安全.

Additionally certain language features allow the compiler in C# and Java to make assumptions about your code that allows it to optimize certain parts away that just aren't safe for the C/C++ compiler to do. When you have access to pointers there's a lot of optimizations that just aren't safe.

此外,Java 和 C# 可以比 C++ 更有效地进行堆分配,因为垃圾收集器和您的代码之间的抽象层允许它一次完成所有堆压缩(一个相当昂贵的操作).

Also Java and C# can do heap allocations more efficiently than C++ because the layer of abstraction between the garbage collector and your code allows it to do all of its heap compression at once (a fairly expensive operation).

现在我不能在下一点上代表 Java,但我知道,例如,当 C# 知道方法体为空时,它实际上会删除方法和方法调用.它将在您的整个代码中使用这种逻辑.

Now I can't speak for Java on this next point, but I know that C# for example will actually remove methods and method calls when it knows the body of the method is empty. And it will use this kind of logic throughout your code.

如您所见,某些 C# 或 Java 实现速度更快的原因有很多.

So as you can see, there are lots of reasons why certain C# or Java implementations will be faster.

现在说了这么多,可以在 C++ 中进行特定的优化,这将击败您可以用 C# 做的任何事情,尤其是在图形领域以及任何您接近硬件的时候.指针在这里创造奇迹.

Now this all said, specific optimizations can be made in C++ that will blow away anything that you could do with C#, especially in the graphics realm and anytime you're close to the hardware. Pointers do wonders here.

因此,根据您所写的内容,我会选择其中一种.但是如果你写的东西不依赖于硬件(驱动程序、视频游戏等),我不会担心 C# 的性能(再次不能谈论 Java).它会做得很好.

So depending on what you're writing I would go with one or the other. But if you're writing something that isn't hardware dependent (driver, video game, etc), I wouldn't worry about the performance of C# (again can't speak about Java). It'll do just fine.

Java 方面,@Swati 指出一篇好文章:

One the Java side, @Swati points out a good article:

https://www.ibm.com/developerworks/library/j-jtp09275

这篇关于C++ 性能与 Java/C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆