Exploring who the C # class and struct are faster -- Cross-assembly call

Source: Internet
Author: User
Tags intel core i5

 

The previous three tests: the http://www.bkjia.com/kf/201110/106556.html is within the same project, both in the same assembly. So what about cross-assembly calls?

 

To ensure maintainability, we encapsulate some common operations in the class library. This class library is referenced during actual project development so that multiple projects exist in the solution. After compilation, it will be a major exe and several dll files. The code in the main exe assembly will call the dll assembly, which forms a cross-assembly call.

The performance of cross-assembly calls can be analyzed to optimize the design of the Class Library Architecture.


I. Test Methods

In fact, the process is very simple --
1. Open VS2005 again. Or close the solution.
2. Create a "Windows application" project. For example, "TryPointerCallns2005 ".
3. Add a project and select "TryPointerCall2005 ". Now there are two projects in the solution.
4. Select the project (TryPointerCallns2005) created at step 1, add the reference, and add "TryPointerCall2005.
5. Adjust project properties to allow Insecure code.
6. Create a new class (TestCall) and Copy all the test code (note that only the test code is copied, and IPointerCall and PointerCall are not copied ).
7. Modify the window interface and call the TestCall test code.


Ii. Test Environment

Compiler --
VS2005: Visual Studio 2005 SP1.
The above compiler is used to compile the Release program, and the maximum speed is optimized.

Machine --
HP CQ42-153TX
Processor: Intel Core i5-430M (2.26 GHz, Turbo 2.53 GHz, 3 MB L3)
Memory Capacity: 2 GB (DDR3-1066)


Iii. Test Results

The test result is as follows --
= PointerCallTool. TryIt () =
Hard coding. Stack variable: 162
Hard coding. stack allocation: 168
Hard-coded. Static: 164
Static call: 160
64 hard coding. Stack variable: 161
64 hard coding. stack allocation: 160
64 hard encoding. Static: 160
64 static call: 162
Call the derived class: 1118
Call sealing class: 1049
Call struct: 1069
Call base class: 1128
Call the interface of the derived class: 1132
Call the sealed class interface: 1135
Interface for calling struct: 1453
Base class generic call derived class: 1769
Base class generic call base class: 1774
Interface generic call derived class: 2230
Interface generic call sealing class: 2240
Interface generic call struct: 1215
Reference of interface generic call struct: 1127
Interface generic call base class: 2241
Interface generic call the interface of the derived class: 2230
Interface generic call sealing class interface: 2244
Interface generic calling struct interface: 2302


It can be seen that during cross-assembly calls, calling the sealing class and calling struct will not be inline optimized by the real-time Compiler (JIT), but will be called as a common virtual method.

Therefore, for code that requires performance, try to ensure that it is in the same assembly and avoid placing it in the class library project, so as to take advantage of the instant Compiler (JIT) inline optimization opportunity as much as possible.

(End)

Source code download: http://www.bkjia.com/uploadfile/2011/1002/20111002112115356.rar

Column of zyl910

 

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.