Font Class - a big performance eater?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I was curious if anybody else noticed this fact. Everytime you use the font class, especially creating multiple instances of the Font class in an iteration, there is a significant performance hit. The way I noticed was due to a bug in my code. I have a list class where each item is a separate object with various properties, among which a Font. At some point I wanted to test my list with a lot of items so I made a loop that was creating like 10000 items, but I used a constructor that passes a font too and I used new Font(...). To my surprize and astonishement, it took an average of 7 milliisecond for the framework to create each item. I got into a sort of panic, as I thought that my event model was too extensive or I forget to dispose some objects. It turned up that when I removed the Font object from the costructor with a null, the average time went from 7 milliseconds to 0.02 miliseconds!! That is like 350 times faster... Now I am even worried about any place that I might use the font constructor. Did anybody noticed this behavior in other instances? Does anyone know an optimized way of using fonts

thank you
Iulian
 
I don't think this is surprising.

AFAIK the Font has to render a completely new set of characters whenever
it's created by running the code in the font program. True type font glyphs
are bits of code that are run by a software font processor a bit like a jave
machine or emulator. Whenever you create a font, all the glyphs are
processed to create a rasterized version of the font which is adjusted for a
particular font size. This is bound to take time.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Check out February's edition of Well Formed.
Non-client drawing, Graphics Transform stack and Flood-Filling
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

Iulian Ionescu said:
I was curious if anybody else noticed this fact. Everytime you use the
font class, especially creating multiple instances of the Font class in an
iteration, there is a significant performance hit. The way I noticed was due
to a bug in my code. I have a list class where each item is a separate
object with various properties, among which a Font. At some point I wanted
to test my list with a lot of items so I made a loop that was creating like
10000 items, but I used a constructor that passes a font too and I used new
Font(...). To my surprize and astonishement, it took an average of 7
milliisecond for the framework to create each item. I got into a sort of
panic, as I thought that my event model was too extensive or I forget to
dispose some objects. It turned up that when I removed the Font object from
the costructor with a null, the average time went from 7 milliseconds to
0.02 miliseconds!! That is like 350 times faster... Now I am even worried
about any place that I might use the font constructor. Did anybody noticed
this behavior in other instances? Does anyone know an optimized way of using
fonts?
 
Back
Top