Skip to content
This repository was archived by the owner on Jun 4, 2026. It is now read-only.
This repository was archived by the owner on Jun 4, 2026. It is now read-only.

文字列の比較に string.Compare を使うのは遅すぎる #191

Description

@ufcpp

Nameの比較など、文字列の比較をことごとくすべて、0 == string.Compare(x, y)で行っているようですが、これがかなりパフォーマンスネックになっていて困っています。

C# の場合、文字列の比較は==で十分です。また、==string.Compareでは10倍くらい性能差があります。

usingSystem;usingSystem.Diagnostics;usingSystem.Net.Http;usingSystem.Threading.Tasks;classProgram{staticvoidMain(string[]args){vartestData=GetTestData().Result;varsw=newStopwatch();// 試しに、単語列の中からtheの数をカウントするのでパフォーマンス確認// とりあえず100回同じ処理するのにかかる時間を測定。// string.Compare が遅すぎて、100で十分だったconstintN=100;intcount=0;// == で比較// C# の string の == はちゃんと中身の比較。// 最終的に行きつくコードはこれ: https://referencesource.microsoft.com/#mscorlib/system/string.cs,11648d2d83718c5e// 1文字1文字の単純比較。ポインターを使った最適化あり。sw.Start();for(inti=0;i<N;i++){count=0;foreach(varwordintestData){if(word=="the")count++;}}sw.Stop();Console.WriteLine(count);Console.WriteLine(sw.Elapsed);sw.Reset();sw.Start();// string.Compare で比較// こっちはこれ: https://referencesource.microsoft.com/#mscorlib/system/globalization/compareinfo.cs,1356715f78447ed6// 正規化してから比較するかとか、大文字・小文字は区別するかとか、空白を除いて比較するかとか、いろいろ余計なオプションがついてる。// 半端なく遅い。for(inti=0;i<N;i++){count=0;foreach(varwordintestData){// さっきとの違いはこの行の if の中身だけif(0==string.Compare(word,"the"))count++;}}sw.Stop();Console.WriteLine(count);// 結果は一緒。Console.WriteLine(sw.Elapsed);// == と比べて10倍くらい遅かった…}// 何か適当な文字列を取ってきて、スペースでSplitprivatestaticasyncTask<string[]>GetTestData(){varc=newHttpClient();varres=awaitc.GetAsync("https://en.wikipedia.org/wiki/Microsoft");varcontent=awaitres.Content.ReadAsStringAsync();returncontent.Split(' ');}}

しかも、例えばScript_SpriteStudio_DataAnimation の中など、結構高頻度で呼ばれる上にforループの中にこの処理があるため、顕著にパフォーマンス劣化が見られます。

Compareを呼ぶとか余計な真似をせず、素直できれいなコードに書き直すだけで10倍近い改善がみられるので、直してはもらえないでしょうか。

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions