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

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

问题描述

我的理解是,C / C ++产生本地code在一个特定的机器架构上运行。相反,就像在一个虚拟机之上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;奔腾4,酷睿单核或双核;或者支持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编译器,使你的code,允许它以优化的某些部分走,只是不为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更有效地做堆分配++,因为垃圾收集和你的code之间的抽象层允许它做所有的堆COM pression在一次(一个相当昂贵的操作)。

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#,例如将实际删除的方法,当它知道方法的主体是空的方法调用。它会用这样的逻辑在整个code。

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:

<一个href=\"http://www-128.ibm.com/developerworks/java/library/j-jtp09275.html?ca=dgr-jw22JavaUrbanLegends\" rel=\"nofollow\">http://www-128.ibm.com/developerworks/java/library/j-jtp09275.html?ca=dgr-jw22JavaUrbanLegends

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

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