Skip to content

Latias94/UnityCsharpPerformanceTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Unity C# 代码性能测试

环境

  • Unity 2019.3.0f5 (代码其实跟版本没啥关系)
  • api compatibility level:.net 4.x
  • enable unsafe code

介绍

测试下 C# 代码在不同写法下的性能差异。

例如处理 int.Parse 的异常:

public new const int DefaultRepetitions = 10;
public const int ListSize = 1000;

protected override bool MeasureTestA()
{
    for (int i = 0; i < Iterations; i++)
    {
        for (int j = 0; j < ListSize; j++)
        {
            try
            {
                int.Parse(numbers[j]);
            }
            catch (FormatException)
            {
            }
        }
    }

    return true;
}

protected override bool MeasureTestB()
{
    for (int i = 0; i < Iterations; i++)
    {
        for (int j = 0; j < ListSize; j++)
        {
            var success = int.TryParse(numbers[j], out int number);
        }
    }

    return true;
}

UI 上统计的是 Stopwatch 的计时

Profiler 也能看到三个测试的统计,其中包含了 Stopwatch 的 allocation,要更准确的数据可以自行去除 Stopwatch。

参考

About

Unity C# 代码性能测试

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages