三种方式实例化一个类的性能情况

2018-06-17 19:22:15来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

源内容:http://www.cnblogs.com/shouce/p/5558095.html#undefined

下面的内容是根据“源内容”进行了整改、补充。

三种方式实例化一类,包括无参数构造形式与有参数构造形式性能测试

  1. 使用new关键字创建类实例(常用方式)。
  2. 使用Activator激活器类创建类实例(Activator用以在本地或从远程创建对象类型,或获取对现有远程对象的引用)。
  3. 使用Assembly程序集创建类实例(Assembly表示一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行库应用程序构造块。该类可以加载程序集、浏览程序集的元数据和构成部分、发现程序集中包含的类型以及创建这些类型的实例)。

测试环境:

  1) Visual studio 2015 Community

  2) Windows 10 Profession

  3) Memory 6G,CPU Inter-Core-i3-3220 @ 3.3GHz

测试代码如下,分别为接口、接口实现类、实例化类的创建、执行测试方法

/// <summary>
/// 职业、行业
/// </summary>
public interface IProfession
{
    string Name { get; }
    string MostMeaningThing();
}
/// <summary>
/// 工人、蓝领
/// </summary>
public class Worker : IProfession
{
    public Worker(string name)
    {
        Name = name;
    }

    public Worker()
    {
        
    }

    public string Name { get; private set; }

    public string MostMeaningThing()
    {
        return "挖掘机哪家强山东找蓝翔";
    }
} 
public class InstanceClass
{
    //可根据自己的项目名称进行修改
    private string className = "TestProblem.Worker";
    //执行次数
    private int time = 100000;
    /// <summary>
    /// 是否执行有参数构造函数
    /// </summary>
    private bool hasParameters;

    public InstanceClass(bool hasParameters)
    {
        this.hasParameters = hasParameters;
    }

    /// <summary>
    /// 通过new关键字创建
    /// </summary>
    public void CreateByNew()
    {
        IProfession profession;
        Stopwatch watch = new Stopwatch();
        watch.Start();
        for (int i = 0; i < time; i++)
        {
            profession = hasParameters ? new Worker("工人-New-" + i) : new Worker();
        }
        watch.Stop();
        Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));

    }
    /// <summary>
    /// 通过Activator控制类创建
    /// </summary>
    public void CreateByActivator()
    {
        Type type = Type.GetType(className);
        IProfession profession;
        Stopwatch watch = new Stopwatch();
        watch.Start();
        for (int i = 0; i < time; i++)
        {
            object obj = hasParameters ? Activator.CreateInstance(type, "工人-Activator-" + i) : Activator.CreateInstance(type);
            profession = obj as IProfession;
        }
        watch.Stop();
        Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
    }
    /// <summary>
    /// 通过Assembly控制类创建
    /// </summary>
    public void CreateByAssembly()
    {
        Assembly assembly = Assembly.GetAssembly(Type.GetType(className));
        IProfession profession;
        Stopwatch watch = new Stopwatch();
        watch.Start();
        for (int i = 0; i < time; i++)
        {
            object obj = hasParameters ?
                assembly.CreateInstance(className, true, BindingFlags.Default, null, new object[] { "工人-Assembly-" + i }, CultureInfo.CurrentCulture, null)
                : assembly.CreateInstance(className);
            profession = obj as IProfession;
        }
        watch.Stop();
        Console.Write(watch.ElapsedMilliseconds.ToString().PadLeft(5));
    }
    /// <summary>
    /// For循环,替代代码中多次手写类似的For循环代码
    /// </summary>
    /// <param name="time">循环次数</param>
    /// <param name="action">委托</param>
    public static void ForLoop(int time, Action<int> action)
    {
        for (int i = 0; i < time; i++)
        {
            action(i);
        }
    }
    /// <summary>
    /// For循环,封装For循环代码执行
    /// </summary>
    /// <param name="time">循环次数</param>
    /// <param name="action">委托,函数指针</param>
    public static void ForLoop(int time, Action action)
    {
        for (int i = 0; i < time; i++)
        {
            action();
        }
    }
}
/// <summary>
/// 执行十次的十万个创建类实例
/// </summary>
private static void ExecuteLakh()
{
    bool hasParameters = true;
    Console.WriteLine("实例化一个类({0}参数构造)的性能比较(单位:毫秒)", hasParameters ? "" : "");
    Console.Write("\t\t\t");
    InstanceClass.ForLoop(10, i => Console.Write("{0:G}", (i + 1).ToString().PadLeft(5)));
    Console.WriteLine();
    InstanceClass instanceClass = new InstanceClass(hasParameters);
    Console.Write("CreateByNew".PadRight(24));
    InstanceClass.ForLoop(10, () => instanceClass.CreateByNew());
    Console.WriteLine();
    Console.Write("CreateByActivator".PadRight(24));
    InstanceClass.ForLoop(10, () => instanceClass.CreateByActivator());
    Console.WriteLine();
    Console.Write("CreateByAssembly".PadRight(24));
    InstanceClass.ForLoop(10, () => instanceClass.CreateByAssembly());
    Console.WriteLine();
}

测试结果如下:

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Suggestion(搜索建议)产品和技术

下一篇:C# DataTable的詳細用法