Dot-Net

哪些屬性有助於執行時 .Net 性能?

  • September 17, 2008

我正在尋找可用於通過向載入程序、JIT 編譯器或 ngen 提供提示來確保我的 .Net 應用程序的最佳執行時性能的屬性。

例如,我們有DebuggableAttribute應該設置為不調試並且不禁用優化以獲得最佳性能。

[Debuggable(false, false)]

還有其他我應該知道的嗎?

Ecma-335 在附件 F“不精確的錯誤”中為寬鬆的異常處理(所謂的 e-relaxed 呼叫)指定了更多的 CompilationRelaxations,但它們尚未被 Microsoft 公開。

其中特別提到了 CompilationRelaxations.RelaxedArrayExceptions 和 CompilationRelaxations.RelaxedNullReferenceException。

當您在 CompilationRelaxationsAttribute 的 ctor 中嘗試一些整數時會發生什麼會很有趣;)

另一個:文字字元串(在原始碼中聲明的字元串)預設情況下嵌入到一個池中以節省記憶體。

string s1 = "MyTest"; 
string s2 = new StringBuilder().Append("My").Append("Test").ToString(); 
string s3 = String.Intern(s2); 
Console.WriteLine((Object)s2==(Object)s1); // Different references.
Console.WriteLine((Object)s3==(Object)s1); // The same reference.

儘管在多次使用相同的文字字元串時它可以節省記憶體,但維護池會花費一些 cpu,並且一旦將字元串放入池中,它就會一直呆在那裡直到程序停止。

使用CompilationRelaxationsAttribute您可以告訴 JIT 編譯器您真的不希望它實習所有文字字元串。

[assembly: CompilationRelaxations(CompilationRelaxations.NoStringInterning)]

引用自:https://stackoverflow.com/questions/70150