Silverlight application performance – refresh rate & CPU usage

July 17, 2008

You just wrote great Silverlight application? How about CPU usage? Do you have pretty static application UI and it still consumes 25-30% of your CPU? Probably you have to control how fast Silverlight should redraw the UI.

To do it, you need to specify MaxFrameRate property in object initialization, like follows:

   1: <asp:Silverlight ID="Xaml1" runat="server" 
   2:     Source="~/ClientBin/ShowFPS.xap" 
   3:     MinimumVersion="2.0.30523" 
   4:     Width="100%" Height="100%"             
   5:     MaxFrameRate="30" 
   6: />

Default value is 60fps, which is pretty high and will made animations looks very smooth. But if you application doesn’t have much animations, or your UI is not changing much? Why not limit it to some acceptable value? Even if you application play video stream the MaxFrameRate shouldn’t be much faster, than encoded stream rate. NTSC video encoded at 29.92fps, average on-demand video streams encoded at 15fps – why not limit your application at 30fps? To demonstrate the difference I’ve wrote sample application, with VERY dynamic UI: it has bouncing ball (which is actually canvas with many path objects inside) animation and video (Vista’s sample “Butterfly.wmv” encoded at 29fps) played at a background. I’m also have some storyboard running which actually enables me to move ball canvas and calculate/update framerate.

Here is unrestricted framerate screenshot:

60fps

59-60fps according to calculations, 25-30% CPU.

Here is the same application with MaxFrameRate applied:

30fps

29-30fps(exactly like we wanted), 12-15% CPU load. Video is very smooth, animation is also smooth, bit plays slower.

Here is same application at 15fps:

15fps

14-15fps(exactly like we wanted), 6-10% CPU load. Video is still pretty smooth, but “jumps” sometime. For local played file it could be visible, but for network stream it is pretty acceptable.

So, next time when writing Silverlight application, consider specifying MaxFrameRate to ease on CPU.

 

Another nice feature of Silverlight plugin – show actual/desired framerate. To enable it add EnableFrameRateCounter=”true” in object definition:

   1: <asp:Silverlight ID="Xaml1" runat="server" 
   2:     Source="~/ClientBin/ShowFPS.xap" 
   3:     MinimumVersion="2.0.30523" 
   4:     Width="100%" Height="100%" 
   5:     EnableFrameRateCounter="true"            
   6:     MaxFrameRate="30" 
   7: />

(*) This will work only in IE.

 

Here is couple more of performance tips:

1. IsWindowless =”false” is faster. Unless you application requires overlay of HTML content on top of Silverlight, don’t turn it on.

2. Use solid/opaque background for Silverlight HTML object – it is faster.

3. Try to avoid usage of transparent/semi-transparent colors for XAML controls.

4. Use Visibility=”Collapsed” instead of Opacity=0 to hide XAML control.

 

That’s it for now.

 

Implementation of Silverlight FPS counter and/or application sources could be found here.

 

Enjoy,

Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

6 comments

  1. BoraJuly 17, 2008 ב 20:10

    Cool!
    I’ve been looking on different tips on SL object for long!!
    Thanks

    Reply
  2. kikusJune 13, 2010 ב 23:30

    отличный пост, автор пиши ещё

    Reply
  3. blogs.microsoft.co.ilMay 26, 2011 ב 16:44

    Silverlight application performance refresh rate amp cpu usage.. OMG! 🙂

    Reply
  4. Lewis Lerud July 1, 2011 ב 17:39

    Exceptional post. I desire to thank you for this informative read, I truly appreciate sharing this good post. Keep up your work.

    Reply
  5. Lewis Lerud July 5, 2011 ב 16:54

    The things you point out is smart, however I still take reservations about this problem ~

    Reply
  6. VargasJanuary 26, 2013 ב 10:12

    I am sure this article has touched all the internet users, its
    really really good piece of writing on building up new web site.

    Reply