私のアプリケーションでは、私はオブジェクトの大きな定数(実際にはstatic readonly
)配列が必要です。配列は、型の静的コンストラクタで初期化されます。アレイの初期化は最も速いですか?
アレイには数千ものアイテムが含まれています。タイプが最初に使用されると、私のプログラムは深刻な減速を経験します。私は、C#で大規模な配列を素早く初期化する方法があるかどうかを知りたいと思います。
public static class XSampa {
public class XSampaPair : IComparable<XSampaPair> {
public XSampaPair GetReverse() {
return new XSampaPair(Key, Target);
}
public string Key { get; private set; }
public string Target { get; private set; }
internal XSampaPair(string key, string target) {
Key = key;
Target = target;
}
public int CompareTo(XSampaPair other) {
if (other == null)
throw new ArgumentNullException("other",
"Cannot compare with Null.");
if (Key == null)
throw new NullReferenceException("Key is null!");
if (other.Key == null)
throw new NullReferenceException("Key is null!");
if (Key.Length == other.Key.Length)
return string.Compare(Key, other.Key,
StringComparison.InvariantCulture);
return other.Key.Length - other.Key;
}
}
private static readonly XSampaPair[] pairs, reversedPairs;
public static string ParseXSampaToIpa(this string xsampa) {
// Parsing code here...
}
public static string ParseIpaToXSampa(this string ipa) {
// reverse code here...
}
static XSampa() {
pairs = new [] {
new XSampaPair("a", "\u0061"),
new XSampaPair("b", "\u0062"),
new XSampaPair("b_<", "\u0253"),
new XSampaPair("c", "\u0063"),
// And many more pairs initialized here...
};
var temp = pairs.Select(x => x.GetReversed());
reversedPairs = temp.ToArray();
Array.Sort(pairs);
Array.Sort(reversedPairs);
}
}
PS:私は、対応するIPAの文字をUnicode文字列にX-SAMPA発音表記を変換するために、アレイに使用します。
コードを投稿できますか? –
'IEnumerable'を使用することは可能ですか?それで、必要に応じてゆっくりと配列を返すことができますか? –
@jbの解決策は良いですが、コードを変更したくない場合は、アプリケーションの起動時にそれを初期化することができます。スプラッシュ画面... –