• Class 创建性能测试


    using System;
    using System.Collections.Generic;
    using System.Diagnostics;

    namespace ConsoleApplication5_Inject
    {
    class Entity { }

    class Program
    {
    staticvoid Main(string[] args)
    {
    #region Run1
    Stopwatch sw
    =new Stopwatch();
    sw.Start();
    for (int i =0; i <100000; i++)
    {
    Run1();
    }
    sw.Stop();
    Console.WriteLine(sw.Elapsed.Milliseconds);
    #endregion

    #region Run2
    sw
    =new Stopwatch();
    sw.Start();
    for (int i =0; i <100000; i++)
    {
    Run2();
    }
    sw.Stop();
    Console.WriteLine(sw.Elapsed.Milliseconds);
    #endregion

    #region Run3
    sw
    =new Stopwatch();
    sw.Start();
    for (int i =0; i <100000; i++)
    {
    Run3();
    }
    sw.Stop();
    Console.WriteLine(sw.Elapsed.Milliseconds);
    #endregion

    #region Run4
    sw
    =new Stopwatch();
    sw.Start();
    for (int i =0; i <100000; i++)
    {
    Run4
    <Entity>();
    }
    sw.Stop();
    Console.WriteLine(sw.Elapsed.Milliseconds);
    #endregion

    Console.ReadKey();
    }

    staticvoid Run1()
    {
    new Entity();
    }

    staticvoid Run2()
    {
    Activator.CreateInstance(
    typeof(Entity));
    }

    staticvoid Run3()
    {
    Activator.CreateInstance
    <Entity>();
    }

    staticvoid Run4<T>() where T : new()
    {
    new T();
    }
    }
    }

    跑10万次 结果:

    1. 3ms

    2. 220ms

    3. 117ms

    4. 121ms

  • 相关阅读:
    javascript之Number
    javascript之window对象
    javascript全局对象
    javascript之尺寸,位置,溢出
    javascript之DOM操作
    javascript之全局函数
    javascript之Error
    javascript之url转义escape()、encodeURI()和decodeURI()
    javascript之Boolean
    javascript之Arguments
  • 原文地址:https://www.cnblogs.com/LinFx/p/2123676.html
Copyright © 2020-2023  润新知