管理C ++ VS未管理/本地C ++的性能 [英] Performance of Managed C++ Vs UnManaged/native C++

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

问题描述

我编写一个非常高性能的应用程序,每毫秒处理和处理数百个事件。

I am writing a very high performance application that handles and processes hundreds of events every millisecond.

非托管C ++比托管c ++快吗?为什么?

Is Unmanaged C++ faster than managed c++? and why?

托管C ++处理CLR而不是操作系统,CLR负责内存管理,这简化了代码,并且可能比由a程序员在非托管C ++?还是有其他原因?
当使用managed时,如何避免动态内存分配,如果它对程序员是透明的并且由CLR处理,会导致性能损失?

Managed C++ deals with CLR instead of OS and CLR takes care of memory management, which simplifies the code and is probably also more efficient than code written by "a programmer" in unmanaged C++? or there is some other reason? When using managed, how can one then avoid dynamic memory allocation, which causes a performance hit, if it is all transparent to the programmer and handled by CLR?

回到我的问题,管理C ++在速度上比非托管C ++更高效,为什么?

So coming back to my question, Is managed C++ more efficient in terms of speed than unmanaged C++ and why?

推荐答案

没有人回答这个。作为一个真正的一般规则,本机代码通常会更快,但是1)并不总是这样,2)有时候差异太小而不关心,3)代码写得有多好通常会比管理对象和非托管对象有更大的差异。

There is no one answer to this. As a really general rule, native code will usually be faster, but 1) that's not always the case, 2) sometimes the difference is too small to care about, and 3) how well the code is written will usually make more difference than managed vs. unmanaged.

编辑:托管代码在虚拟机中运行。基本上,你从一个编译器开始生产字节代码作为输出,然后馈送到虚拟机。然后虚拟机解释它或将它重新编译为机器码并执行它。无论哪种方式,您都添加了一些开销。

Managed code runs in a virtual machine. Basically, you start with a compiler that produces byte codes as output, then feed that to the virtual machine. The virtual machine then either interprets it or re-compiles it to machine code and executes that. Either way, you've added some overhead.

VM还使用垃圾回收器。垃圾收集器具有与手动管理存储器相当不同的特性。对于大多数手动管理器,分配内存是相当昂贵的。释放内存相当便宜,但对于您释放的项目数量大致是线性的。

The VM also uses a garbage collector. Garbage collectors have rather different characteristics from manually managing memory. With most manual managers, allocating memory is fairly expensive. Releasing memory is fairly cheap, but roughly linear on the number of items you release.

使用垃圾回收器,分配内存通常非常便宜。对于典型的(复制)收集器,释放内存的成本主要取决于已经分配并且仍然(至少可能)正在使用的对象的数量。

With a garbage collector, allocating memory is typically very cheap. With a typical (copying) collector, the cost of releasing memory depends primarily upon the number of objects that have been allocated and are still (at least potentially) in use.

分配本身也不同。在本机C ++中,通常在堆栈上创建大多数对象,其中分配释放内存是非常便宜的。在托管代码中,通常会动态分配更大百分比的内存,并在其中进行垃圾回收。

The allocations themselves also differ though. In native C++, you typically create most objects on the stack, where both allocating and releasing memory is extremely cheap. In managed code, you typically allocate a much larger percentage of memory dynamically, where it's garbage collected.

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

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