Reflection performance test

Source: Internet
Author: User

Source code: Testreflection.rar
1. Create an empty solution.
2. Add three class library projects to the solution: idal, Dal, and BLL.
3. Create a web project for testing.

4. Add the ireflection interface to idal:
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace idal
{
Public interface ireflection
{
Void test ();
}
}

5. Add the reflectionda class to the Dal:
Using system;
Using system. Collections. Generic;
Using system. text;
Using idal;

Namespace dal
{
Public class reflectionda: ireflection
{
Public void test ()
{
Return;
}
}
}

6. Add a test class to BLL:
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. reflection;
Using idal;

Namespace BLL
{
/// <Summary>
/// Test the Reflection Efficiency
/// </Summary>
/// <Remarks>
/// Prepared by: ☆chatting with others ☆
/// Time: 2007-03-26
/// </Remarks>
Public class reflectionbl
{
/// <Summary>
/// Create an instance for each reflection and call the class method using the interface
/// </Summary>
/// <Param name = "count"> Number of method calls </param>
/// <Returns> time taken to call a method </returns>
Public timespan testfrominterface (INT count)
{
// Reflection call
Datetime startr = datetime. now;
For (INT I = 0; I <count; I ++)
{
Assembly = assembly. LoadFile (getdllpath ());
Type type = assembly. GetType ("Dal. reflectionda ");
Constructorinfo coninfo = type. getconstructor (New Type [] {});
Object OBJ = coninfo. Invoke (new object [] {});

Ireflection reflection = (ireflection) OBJ;
Reflection. Test ();
}

Return datetime. Now-startr;
}

/// <Summary>
/// Create an instance for each reflection, and then call the class method for reflection
/// </Summary>
/// <Param name = "count"> Number of method calls </param>
/// <Returns> time taken to call a method </returns>
Public timespan testfromreflection (INT count)
{
// API-based reflection call
Datetime startr = datetime. now;
For (INT I = 0; I <count; I ++)
{
Assembly = assembly. LoadFile (getdllpath ());
Type type = assembly. GetType ("Dal. reflectionda ");
Constructorinfo coninfo = type. getconstructor (New Type [] {});
Object OBJ = coninfo. Invoke (new object [] {});

Object RTN;
RTN = type. getmethod ("test"). Invoke (OBJ, new object [] {});
}

Return datetime. Now-startr;
}

/// <Summary>
/// Instantiate a class with the New Keyword each time and call its Method
/// </Summary>
/// <Param name = "count"> Number of method calls </param>
/// <Returns> time taken to call a method </returns>
Public timespan test (INT count)
{
Datetime startr = datetime. now;
For (INT I = 0; I <count; I ++)
{
Dal. reflectionda Dal = New DAL. reflectionda ();
Dal. Test ();
}
Return datetime. Now-startr;
}

///


// obtain the path of the dynamic link library
///
// path of the dynamic link library
private string getdllpath ()
{< br> return @ "F: \ localhost \ testreflection \ Dal \ bin \ debug \ Dal. DLL ";
}

///


// use reflection to create an object, then, use the interface call method
///
/// time spent calling the method
// time taken to call a method
Public timespan testfrominterface1 (INT count)
{

Assembly = assembly. loadFile (getdllpath ();
type = assembly. getType ("Dal. reflectionda ");
constructorinfo coninfo = type. getconstructor (New Type [] {});
Object OBJ = coninfo. invoke (new object [] {});

// Reflection call
Datetime startr = datetime. now;
For (INT I = 0; I <count; I ++)
{
Ireflection reflection = (ireflection) OBJ;
Reflection. Test ();
}

Return datetime. Now-startr;
}

///


/// create an instance of the object first, re-reflection of the call method
///
/// time spent on calling the method
/// time taken to call a method
Public timespan testfromreflection1 (INT count)
{< br> // API-based reflection call

Assembly = assembly. loadFile (getdllpath ();
type = assembly. getType ("Dal. reflectionda ");
constructorinfo coninfo = type. getconstructor (New Type [] {});
Object OBJ = coninfo. invoke (new object [] {});

Datetime startr = datetime. now;
For (INT I = 0; I <count; I ++)
{
Object RTN;
RTN = type. getmethod ("test"). Invoke (OBJ, new object [] {});
}

Return datetime. Now-startr;
}
}
}

The test results on my local machine are as follows:

Frequent object instance creation:
Reflection Method: 00: 00: 01.9843750
Interface call: 00: 00: 01.9687500

Create an object instance in advance:
Reflection Method: 00: 00: 00.3750000
Interface call: 00: 00: 00
Direct call: 00: 00: 00

Machine configuration: AMD 3000 + 64-bit/1g memory/MS win2k3

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.