Comparison of execution efficiency between managed code and non-managed code

Source: Internet
Author: User

I. First, I would like to answer a question: is managed code (. net) slower than unmanaged code (vc ++?

If you use the above question to ask everyone, basically everyone will answer it, and it will be slow! Are they correct? No, it is incorrect. The problem is that most people think. Net is just a Runtime Library-based framework, like Java or VB, or they even think. Net uses virtual machine systems like Java. They did not take into account the program itself, did not take into account what the program is used for, and did not take into account the speed of access to the network or disk. Simply put, they do not think at all!

. NET is not like the Runtime Library (VB or Java ). It is a well-conceived framework in which Microsoft has made great efforts to ensure its good operation. In this article, I will show you some code that requires a lot of computation and compile it into hosted and unmanaged code. Then I will measure the performance of the two databases separately. You will see that it is not because the. net program is automatically slower than the c ++ program. In fact, in some cases, the managed code is even faster than the unmanaged code.

Basically everyone knows that all. Net languages will be compiled into an intermediate language called IL assembly. However, many people do not know how the computer executes the intermediate code, or even understand it incorrectly.
JIT is an important part of. NET program running. The misunderstanding I just mentioned is that many people (definitely not a minority, I have asked many c ++ programmers, and there are 9 such ideas) JIT is actually something similar to Java VM. It is an Interpreter that reads the IL assembly code at runtime and then simulates it into x86 code (also known as a virtual machine ). But in fact,. NET uses more advanced technologies. After the. Net program is added to the memory, when a segment of IL code is run for the first time, the JIT compiler will compile the entire IL code and then execute it. That's why the first time the. NET program was started slowly! With. NET Library, Microsoft also comes with a tool, you can advance. all the IL code of the. NET program is compiled and stored in the cache. As a result, this program is exactly the same as that compiled by c ++, with no difference, the running time can also be separated from JIT (do not confuse it here, it is not to say that it can be separated. NET Library, but does not need to be compiled in real time ). Therefore, do not confuse. NET and Java. The two operational efficiency is not a level at all!

 

Ii. experiment comparison

As a test algorithm, we select FFT (Fast Fourier Transform), an algorithm that converts time-related data (such as music) to its expected frequency information.
There are many types of algorithms. If you search by Google, you will find a lot of algorithms. Here I select Real Discrete Fourier Transform, because it is relatively simple and easy to modify. I copied four copies to test the hosted C ++, C ++/CLI, and C #.

For unmanaged code, I just changed the function name to fourier and added _ declspec (dllexport) for export.
The managed code changes slightly:

* The method parameter is changed to a hosted Array, and Array: Length is used instead of the additional Length parameter.
* Where trigonometric functions are involved, use the methods in the Math class.
* Algorithms are exported as static members of a public class.

Then I converted the hosted c ++ code into c # And only made minor changes (most of them were syntactic and declarative changes)
Finally, I converted the hosted c ++ code into C ++/CLI

Then we will compile several different versions of all versions: unoptimized version, space optimization, and speed optimization.

Result:
I tested these programs on two computers, one with. net 2.0 XP SP2 and the processor with PIII 850,512 MB memory. The other one is Vista build 5321, and the processor is 2 GHz mobile PIV and 1 GB memory. In each test, I take the average value of 100 algorithm operations. The result unit is millisecond, the following figure shows the running result of PIII:
No optimization, space optimization, and Speed Optimization
Unmanaged 92.88 ± 0.09 88.23 ± 0.09 68.48 ± 0.03
Managed C ++ 72.89 ± 0.03 72.26 ± 0.04 71.35 ± 0.06
C ++/CLI 73.00 ± 0.05 72.32 ± 0.03 71.44 ± 0.04
C # Managed 72.21 ± 0.04 69.97 ± 0.08

Results of the PIV computer:
No optimization, space optimization, and Speed Optimization
Unmanaged 45.2 ± 0.1 30.04 ± 0.04 23.06 ± 0.04
Managed C ++ 23.5 ± 0.1 23.17 ± 0.08 23.36 ± 0.07
C ++/CLI 23.5 ± 0.1 23.11 ± 0.07 23.80 ± 0.05
C # Managed 23.7 ± 0.1 22.78 ± 0.03

It can be seen that the efficiency of the unmanaged code varies greatly in different optimization schemes. The optimization speed of the PIII is 35% lower than that of the optimization, and the same is true for the PIV. This crude statistics shows that, no matter which optimization solution, management code is not significantly different in terms of running efficiency. The compiler and connector do not affect the running efficiency. I will explain it later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.