G
Guest
Recently I've been investigating performance problems with painting in one of
my controls. I've ended up with a trivial sample which illustrates strange
behavior of Graphics.DrawIcon(...).
I made a simple form and tried to draw an icon multiple times:
private void button1_Click(object sender, EventArgs e)
{
Icon icon = Resource1.MyIcon;
Rectangle rect = new Rectangle(10, 10, 16, 16);
using (Graphics g = this.CreateGraphics())
{
for (int loop = 0; loop < 10; loop++)
{
DateTime dt1 = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
g.DrawIcon(icon, rect);
}
DateTime dt2 = DateTime.Now;
TimeSpan elapsed = dt2.Subtract(dt1);
Trace.WriteLine(elapsed.TotalMilliseconds.ToString());
}
}
}
So, I draw the icon 10 000 times, measuring the time needed for each
thousand. Here is what I typically get from Trace:
578,0584
874,8992
999,8848
1171,74
1390,4648
1515,4504
1624,8128
1859,1608
2015,3928
2109,132
It seems that each new thousand of DrawIcon() operations is done noticeably
slower! If I repeat it more times, I get even worse results...
Sounds like a bug to me (maybe poor caching of something, so each time a
lookup over a bigger set of things is performed). I've also noticed that if a
make a long-running loop like this, the consumed memory slowly increases.
Any explanations?
JakubS
PS: Currently I worked around the problem by doing icon.ToBitmap() and using
DrawImage(...) instead - it is much faster.
my controls. I've ended up with a trivial sample which illustrates strange
behavior of Graphics.DrawIcon(...).
I made a simple form and tried to draw an icon multiple times:
private void button1_Click(object sender, EventArgs e)
{
Icon icon = Resource1.MyIcon;
Rectangle rect = new Rectangle(10, 10, 16, 16);
using (Graphics g = this.CreateGraphics())
{
for (int loop = 0; loop < 10; loop++)
{
DateTime dt1 = DateTime.Now;
for (int i = 0; i < 1000; i++)
{
g.DrawIcon(icon, rect);
}
DateTime dt2 = DateTime.Now;
TimeSpan elapsed = dt2.Subtract(dt1);
Trace.WriteLine(elapsed.TotalMilliseconds.ToString());
}
}
}
So, I draw the icon 10 000 times, measuring the time needed for each
thousand. Here is what I typically get from Trace:
578,0584
874,8992
999,8848
1171,74
1390,4648
1515,4504
1624,8128
1859,1608
2015,3928
2109,132
It seems that each new thousand of DrawIcon() operations is done noticeably
slower! If I repeat it more times, I get even worse results...
Sounds like a bug to me (maybe poor caching of something, so each time a
lookup over a bigger set of things is performed). I've also noticed that if a
make a long-running loop like this, the consumed memory slowly increases.
Any explanations?
JakubS
PS: Currently I worked around the problem by doing icon.ToBitmap() and using
DrawImage(...) instead - it is much faster.