コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】.Replace( "\\", "/" ) よりも .Replace( '\\', '/' ) の方が速い

検証結果

検証用スクリプト

using UnityEngine;
using UnityEngine.Profiling;

public class Example : MonoBehaviour
{
    private void Update()
    {
        const string path  = "Assets\\material.mat";
        const int    count = 100000;

        Profiler.BeginSample( "\"" );

        for ( var i = 0; i < count; i++ )
        {
            _ = path.Replace( "\\", "/" );
        }

        Profiler.EndSample();

        Profiler.BeginSample( "\'" );

        for ( var i = 0; i < count; i++ )
        {
            _ = path.Replace( '\\', '/' );
        }

        Profiler.EndSample();
    }
}