私は現在、アプリケーションのパフォーマンスの問題を調査しており、次の点を強調しています。
私はクラスを持っています-
public static class CommonIcons
{
...
public static readonly System.Windows.Media.ImageSource Attributes = typeof(CommonIcons).Assembly.GetImageFromResourcePath("Resources/attributes.png");
...
}
テストハーネスとして、このクラスを使用して問題を示す次のコードがあります-
for (int loop = 0; loop < 20000; loop++)
{
// store time before call
System.Windows.Controls.Image image = new System.Windows.Controls.Image
{
Source = CommonIcons.Attributes,
Width = 16,
Height = 16,
VerticalAlignment = VerticalAlignment.Center,
SnapsToDevicePixels = true
};
// store time after call
// log time between before and after
}
ループの開始時の時間差は0.001秒未満ですが、20000が経過すると、これは0.015秒に増加します。
静的メンバーを使用せず、アイコンを直接参照する場合、パフォーマンスに影響はありません。
for (int loop = 0; loop < 20000; loop++)
{
// store time before call
System.Windows.Controls.Image image = new System.Windows.Controls.Image
{
Source = typeof(CommonIcons).Assembly.GetImageFromResourcePath("Resources/attributes.png"),
Width = 16,
Height = 16,
VerticalAlignment = VerticalAlignment.Center,
SnapsToDevicePixels = true
};
// store time after call
// log time between before and after
}
しかし、私の実際のプログラムでは、呼び出しのたびにイメージソースを作成したくないので(ガベージコレクションまでメモリを増やします)、静的メンバーが使用されるのはなぜですか。しかし、私もパフォーマンスヒットで生きることはできません。
元のコードがこのパフォーマンスヒットを引き起こしている理由を誰かが説明できますか?また、私がやろうとしていることに対するより良い解決策はありますか?
ありがとう