. Net c # Performance Comparison of Three Ways to instantiate a class,
Content
- How to instantiate a class
- Instantiate a class with the New Keyword
- Instantiate a class with Activator
- Instantiate a class with Assembly
- Performance Comparison
- Environment
- Comparison
- Analysis
- Code
It is useful to dynamically instantiate a class when developing an application. You can create an instance of the class by providing a string name of the class. If the classes to be instantiated all inherit the same interface (such as IPerson in this example), the instantiated object type conversion can be assigned to this interface. This is convenient. Otherwise, you must use the swtich-case statement (which is estimated to be long ).
The following question is how to instantiate the performance of a class.
Previously, I usedAssemblyMany. After writing this article, I found it inappropriate.
How to instantiate a class
- Instantiate a class with the New Keyword
NewKeywords are used to create objects and call constructors. Is the most common way to instantiate a class.
- Instantiate a class with Activator
ActivatorIt is used to create object types locally or remotely, or to obtain references to existing remote objects. ItsCreateInstanceMethod To create an instance of the type defined in the dataset.
- Instantiate a class with Assembly
AssemblyIndicates an assembly. It is a reusable, version-free, and self-describing block of the Common Language Runtime application. This class can load the Assembly, browse the metadata and components of the assembly, discover types contained in the Assembly, and create instances of these types.
The recommended method for loading an assembly is to useLoadMethod.GetTypeMethods can be used to search for specific types in a dataset.CreateInstanceThe method can be used to search for and create instances of the type in the Assembly.
This article uses these three methods to create a class instance.
Performance Comparison Environment
- VS 2008. NET Framework 3.5
- Intel CPU Duo T5450 1.66 GHz
- Memory 2G
Comparison
Assume that all classes used in the Demo are in the same assembly.
using System;
namespace InstancePerformance
{
class Program
{
static void Main(string[] args)
{
Console. WriteLine ("compare the performance of instantiating a class (unit: milliseconds )");
Console.Write(" ");
for (int i = 1; i <= 10; i++)
Console.Write("{0:G}", i.ToString().PadLeft(5));
Console.Write("\n");
// New Key
Console.Write("InstanceByNew".PadRight(20));
for (int i = 1; i <= 10; i++)
InstanceByNewKey.Create();
Console.Write("\n");
// Activator
Console.Write("InstanceByActivator".PadRight(20));
for (int i = 1; i <= 10; i++)
InstanceByActivator.Create();
Console.Write("\n");
// Assembly
Console.Write("InstanceByAssembly".PadRight(20));
for (int i = 1; i <= 10; i++)
InstanceByAssembly.Create();
Console.Write("\n");
Console.ReadKey();
}
}
}
Public class IPerson
{
}
public class Person : IPerson
{
public string Name { get; set; }
public Person()
{
}
public Person(string name)
{
this.Name = name;
}
}
Code snippet 2: instantiate with the New Keyword
public class InstanceByNewKey
{
/// <summary>
/// Direct call
/// </summary>
public static void Create()
{
IPerson person = null;
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100000; i++)
person = new Person("My" + i);
watch.Stop();
Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
}
}
In addition, if the code segment is written as follows, it is unnecessary. Other code segments are similar.
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100000; i++)
IPerson person = new Person("My" + i);
watch.Stop();
Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
Code snippet 3: instantiate with Activator
public class InstanceByActivator
{
/// <summary>
/// Instantiate reflection
/// </summary>
public static void Create()
{
Type type = Type.GetType("InstancePerformance.Person");
IPerson person = null;
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100000; i++)
{
object obj = Activator.CreateInstance(type);
person = obj as IPerson;
}
watch.Stop();
Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
}
}
Code segment 4: instantiate with Assembly
public class InstanceByNewKey
{
/// <summary>
/// Direct call
/// </summary>
public static void Create()
{
IPerson person = null;
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100000; i++)
person = new Person("My" + i);
watch.Stop();
Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
}
}
Download Demo