• C# new Guid()、Guid.Parse()、Guid.TryParse() 的简单性能对比


    三种方式分别执行一百万次转换用时:
    new Guid():258 ms
    Guid.Parse():269 ms
    Guid.TryParse():267 ms

    可以看出耗时差距微乎其微,并且以上代码在不同硬件环境下所耗时也有些许变化。下面是完整代码供验证使用:

    using System;
    using System.Diagnostics;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                const int count = 1000000;
                string input = "034A8543-892D-41E7-9AE8-46DD6C882A99";
                Console.WriteLine("三种方式分别用时:");
    
                var sw = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    new Guid(input);
                }
                sw.Stop();
                Console.WriteLine($"new Guid():{sw.ElapsedMilliseconds} ms");
    
                sw = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    Guid.Parse(input);
                }
                sw.Stop();
                Console.WriteLine($"Guid.Parse():{sw.ElapsedMilliseconds} ms");
    
                sw = Stopwatch.StartNew();
                for (int i = 0; i < count; i++)
                {
                    Guid.TryParse(input, out var c);
                }
                sw.Stop();
                Console.WriteLine($"Guid.TryParse():{sw.ElapsedMilliseconds} ms");
    
                Console.ReadKey();
            }
        }
    }
    
  • 相关阅读:
    linux磁盘扩容脚本不重启
    编译安装redis
    编译安装nginx
    ansible常用模块
    centos7 yum安装ansible
    centos7 salt操作命令
    centos7 yum安装salt
    keep
    MySQL6
    MySQL5
  • 原文地址:https://www.cnblogs.com/fxck/p/13076777.html
Copyright © 2020-2023  润新知