[C #] determines whether a class implements multiple methods and Performance Analysis for an interface,
IEntity is an interface, and Entity is the class that implements the IEntity interface:
interface IEntity{ int Id { get; set; }}public class Entity : IEntity{ public int Id { get; set; } public string Prop { get; set; }}
Determine whether Entity implements the IEntity interface.
C # how to determine whether a class implements an Interface
In C #, determine whether a class has implemented an interface.
There are five methods:
1.
Entity entity = new Entity ();
If (entity is IEntity)
{
}
2.
Entity entity = new Entity ();
IEntity temp = entity as IEntity;
If (temp! = Null)
{
}
3.
If (typeof (IEntity). IsAssignableFrom (typeof (Entity )))
{
}
4.
If (typeof (Entity). GetInterfaces (). Contains (typeof (IEntity )))
{
}
5.
If (typeof (Entity). GetInterface ("IEntity ")! = Null)
{
}
However, we know that the first two methods need to instantiate the new instance of Entity, and the last three methods need to use reflection, which are relatively performance-consuming operations!
So which of the following methods to determine whether Entity implements the IEntity interface is relatively low in performance consumption?
We have each method run 10 million times: (the following tests are in the Release mode,. net framework 4.5.1 framework)
Stopwatch stopwatch = null; int count = 10000000; Console. writeLine ("execution count {0}", count); stopwatch = Stopwatch. startNew (); for (int I = 0; I <count; I ++) {Entity entity = new Entity (); if (entity is IEntity) {}} Console. writeLine (stopwatch. elapsed); stopwatch = Stopwatch. startNew (); for (int I = 0; I <count; I ++) {Entity entity = new Entity (); IEntity temp = entity as IEntity; if (temp! = Null) {}} Console. writeLine (stopwatch. elapsed); stopwatch = Stopwatch. startNew (); for (int I = 0; I <count; I ++) {if (typeof (IEntity ). isAssignableFrom (typeof (Entity) {}} Console. writeLine (stopwatch. elapsed); stopwatch = Stopwatch. startNew (); for (int I = 0; I <count; I ++) {if (typeof (Entity ). getInterfaces (). contains (typeof (IEntity) {}} Console. writeLine (stopwatch. elapsed); stopwatch = Stopwatch. StartNew (); for (int I = 0; I <count; I ++) {if (typeof (Entity). GetInterface ("IEntity ")! = Null) {}} Console. WriteLine (stopwatch. Elapsed );
Test results:
Run the following 0.1 billion tests:
We can see that the performance consumption of the last three reflection methods is significantly higher than that of the first two methods. So is it true?
Back to the definition of the Entity class above, we found that the Entity class has only two attributes, and the main overhead of instantiating Entity is calculated based on the total length of all fields of the type. Then we increase the Enity attribute to 30, and each method executes 0.1 billion results:
At this time, we found that the performance consumption of the last three methods with reflection has almost never changed, and the time consumed by the first two methods with instantiation operations has doubled!
We continue to add the Enity attribute to 60, and each method executes the result 0.1 billion times:
The number of Enity attributes is increased to 90. The result of executing each method is as follows:
When the attribute of the Entity class reaches 90, the performance consumption required by instantiation is much higher than the performance consumption caused by two typeof.
My conclusion is that when the number of attributes/fields of a class and an interface is small, the performance consumption of a class is: method 2 <method 1 <method 3 <Method 4 <Method 5
When there are too many attributes/fields for a class and an interface, determine whether a class has implemented an interface. The performance consumption is: method 3 <method 2 <method 1 <Method 4 <Method 5
So why is the performance of method 2 superior to method 1? Let's leave it to the garden friends to answer the question: Download the demo.