1
votes

I'm doing performance tuning on a Silverlight WP7 game and I'm observing a weird behavior when running in the Emulator (I have no real device, so I can't tell if it's an emulator artifact).

I am using a Storyboard as main game loop timer, I update the rendering in the Completed event, then restart the animation. The problem is that the Completed event is triggered at very different intervals.

I isolated the problem in a very simple function, called for ex. on a button click (I put this in a totally empty application, there is nothing else going on in the app).

var sw = new Stopwatch();

var animation = new Storyboard();
animation.Duration = TimeSpan.FromMilliseconds(10);
animation.Completed += (s, e) =>
{
    sw.Stop();
    Debug.WriteLine(sw.ElapsedMilliseconds.ToString());
    sw.Reset();
    sw.Start();
    animation.Begin();
};

animation.Begin();

The output I would expect (ideally) is something like

10
10
10
10
...

but instead, this is what I get:

12
15
16
17
17
14
10
10
10
9
11
132
10
20
11
11
10
12

The time between cycles varies quite a bit, but most important there are occasional very long delays (like the 132 above). This happens regardless of the animation Duration, i.e. the actual duration is "centered" towards the set duration, but varies a lot with intermittent long delays.

You can imagine that my game is running quite irregularly, not smooth and uniform at all. Also I noticed that regardless of how simple the render operation is, I cannot go over ~42 FPS.

Question 1: Am I doing some obvious and blatant mistake? Is my approach flawed?

Question 2: Why is the Storyboard duration so inconsistent between runs?

1
Two thoughts: 1) look at what other code is running that could be consuming cycles, as this will interfere with storyboard animations. 2) get a physical WP7 device before you go live - otherwise you won't have any real idea what your apps performance is like. - LBushkin
about 1): there is /no/ other code. The app only has a button that on click executes the function above. Nothing else. - Francesco De Vittori
I would assume there is other software running on your PC and hosting the emulator. It is that which could be causing delays on processes/threads. There is no guarantee that the timer/duration will take exactly the number of milliseconds specified - Matt Lacey
I tested this code on my device -- I get a similar variance to what the emulator shows. I don't know why, but I don't think it's the emulator that's the issue. - JP Alioto
@JP Alioto: thanks for taking the time to test it! Did you get the small variances and the large occasional "spikes" as well? - Francesco De Vittori

1 Answers

2
votes

I am 90% sure is it caused by emulator yet be aware that 10ms is very short interval. Silverlight app is getting refreshed 60 times per second so it is 16,6ms. I recommend you set it to 33 ms or more.

In one of my game I use DispatcherTimer which works fine (for falling bombs :) ) but is not that accurate as I thought. Using storyboard as a timer is better IMO. Basically, if you do not have any other complex animations compositor thread works smooth and your timer is pretty accurate.

As someone with a device to test your code (or buy one).