Simple testing of code running efficiency (compiler, reflection, optimized reflection)

Source: Internet
Author: User

The code is very simple and I will not talk about it much. Paste code and data directly.

1: Compiler call

 

Code
UserCore core = new UserCore ();
Core. GetCount (1, 50000279 );

 

2: Reflection

 

Code
Assembly classSampleAssembly = Assembly. LoadFrom (@ "C: \ SNSService \ Snda. Qidian. SNS. DataCore. dll ");
Type classSampleType = classSampleAssembly. GetType ("Snda. Qidian. SNS. DataCore. UserCore ");
Object instance = Activator. CreateInstance (classSampleType );
Object returnValue1 = classSampleType. InvokeMember ("GetCount", BindingFlags. InvokeMethod | BindingFlags. Public | BindingFlags. Instance, null, instance, new object [] {50000279 });

 

3: optimized reflection

 

Code
Assembly assembly = Assembly. Load ("Snda. Qidian. SNS. DataCore ");
Object ins = null;
Type HereType = null;
MethodInfo method = null;

// Reflection on the module
Foreach (Module m in assembly. GetModules ())
{

Foreach (Type t in m. GetTypes ())
{
If (string. Equals (t. Name, "UserCore "))
{
Foreach (MethodInfo mInfo in t. GetMethods ())
{
If (string. Equals (mInfo. Name, "GetCount "))
{
HereType = t;
Method = mInfo;
Break;
}
}
}

If (method! = Null)
Break;
}

If (method! = Null)
Break;
}

If (! Method. IsStatic)
{
Ins = assembly. CreateInstance (HereType. FullName );
}
Object RetObject = method. Invoke (ins, new object [] {50000279 });

 

Test Data

Column B, C, and D are listed as 1, 2, and the time consumed for running 100 times in three ways

The F, G, and H columns are 1, 2, and the time consumed for one operation

The last row is the average value of each method.

Do you have any ideas.

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.