小编Mot*_*ked的帖子

静态构造函数的性能以及为什么我们无法指定beforefieldinit

我使用以下两个结构来发现速度上的差异:

public struct NoStaticCtor
{
    private static int _myValue = 3;
    public static int GetMyValue() { return _myValue; }
}

public struct StaticCtor
{
    private static int _myValue;
    public static int GetMyValue() { return _myValue; }
    static StaticCtor()
    {
        _myValue = 3;
    }
}

class Program
{
    static void Main(string[] args)
    {
        long numTimes = 5000000000; // yup, 5 billion
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (long i = 0; i < numTimes; i++)
        {
            NoStaticCtor.GetMyValue();
        }
        sw.Stop();
        Console.WriteLine("No …
Run Code Online (Sandbox Code Playgroud)

.net c# clr performance

7
推荐指数
1
解决办法
1688
查看次数

标签 统计

.net ×1

c# ×1

clr ×1

performance ×1