検証用スクリプト
using System;
using System.Diagnostics;
using UnityEngine;
using Debug = UnityEngine.Debug;
public class Example : MonoBehaviour
{
public int m_count = 1_000_000;
private void Start()
{
const string path = "Assets/Example.cs";
const string directoryName = "Assets/";
const string extension = ".cs";
{
var stopwatch = Stopwatch.StartNew();
for ( var i = 0; i < m_count; i++ )
{
path.StartsWith( directoryName );
}
Debug.Log( $"path.StartsWith( directoryName ): {stopwatch.Elapsed.TotalSeconds} 秒 " );
}
{
var stopwatch = Stopwatch.StartNew();
for ( var i = 0; i < m_count; i++ )
{
path.StartsWith( directoryName, StringComparison.Ordinal );
}
Debug.Log( $"path.StartsWith( directoryName, StringComparison.Ordinal ): {stopwatch.Elapsed.TotalSeconds} 秒 " );
}
{
var stopwatch = Stopwatch.StartNew();
for ( var i = 0; i < m_count; i++ )
{
path.EndsWith( extension );
}
Debug.Log( $"path.EndsWith( extension ): {stopwatch.Elapsed.TotalSeconds} 秒 " );
}
{
var stopwatch = Stopwatch.StartNew();
for ( var i = 0; i < m_count; i++ )
{
path.EndsWith( extension, StringComparison.Ordinal );
}
Debug.Log( $"path.EndsWith( extension, StringComparison.Ordinal ): {stopwatch.Elapsed.TotalSeconds} 秒 " );
}
}
}
検証結果
項目 |
StringComparison.Ordinal の指定 |
時間 |
StartsWith |
なし |
0.96 秒 |
StartsWith |
あり |
0.09 秒 |
EndsWith |
なし |
3.69 秒 |
EndsWith |
あり |
0.04 秒 |
関連記事