Performance of Flash/AS3 vs. Silverlight/C#


A few months back, I posted some figures on the performance of Arrays, Vectors and linked lists in ActionScript 3.  Arrays were really slow, vectors much better, and linked lists the best, but all were slow compared to natively compiled C++ code.

Recently I’ve been experimenting with Silverlight and C#, and it generally seems much speedier than Flash and AS3. So I re-did the test I did a few months back, doing repeated element-by-element addition of two long arrays, this time using C#. I also redid the AS3 timing test, since I got a faster MacBook Pro since my last post on this subject. The core of the C# timing test code looks like this:

			int startTime = System.Environment.TickCount;
			for (int j = 0; j < LOOPS; j++)
			{
				for (int k = 0; k < N; k++)
				{
					z[k] = x[k] + y[k];
				}
			}
			int endTime = System.Environment.TickCount;

So here’s how Silverlight/C# stacks up against Flash/AS3:

Approach Time (ms)
AS3, Array 713
AS3, Vector 408
AS3, Linked List 151
C#, Array 70

So for this simple test, C# arrays are over 5x faster than Vectors in AS3, and over double the speed of linked lists. This is only one test, of course, but if this sort of difference in performance is typical, it clearly has big implications on what’s possible on the two platforms. In particular, it makes Silverlight/C# very attractive for real-time audio applications, which require lots of array accessing and number crunching.  [Update 6 April 2011: I also found that an FFT I wrote ran ~4x faster in C# than in AS3. See An FFT in C#].

Tests were run on my MacBook Pro (2.66 GHz Intel Core i7, Mac OS 10.6.6), with Flash Player 10.1.52.14 and Silverlight 4.0.60129.0.

For the full C# timing test code, click “show code” below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace SpeedTest
{
	public partial class MainPage : UserControl
	{
        private const int N = 5000;		// No. of elements in arrays
        private const int LOOPS = 5000;	// No. of times to repeat the calculations

		public MainPage()
		{
			InitializeComponent();

			int elapsed = ArrayTest();
			status.Content = "Elapsed: " + elapsed.ToString(); // Note: 'status' is a 'Label' defined in the project's xaml file.
		}

		/**
		 * Add elements of one array to elements of another
		 * array, and put the results into a third array.
		 */
		private int ArrayTest()
		{
			// Create arrays
			double[] x = new double[N];
			double[] y = new double[N];
			double[] z = new double[N];

			// Initialize them with some values
			for (int k = 0; k < N; k++)
			{
				x[k] = k;
				y[k] = k;
				z[k] = 0.0;
			}

			// Add the values in the two arrays, with result
			// in a third array.  Repeat many times.
			int startTime = System.Environment.TickCount;
			for (int j = 0; j < LOOPS; j++)
			{
				for (int k = 0; k < N; k++)
				{
					z[k] = x[k] + y[k];
				}
			}
			int endTime = System.Environment.TickCount;

			// Return the elapsed time.
			return (endTime - startTime);
		}
	}
}
Advertisements

About Gerry Beauregard

I'm a Singapore-based Canadian software engineer, inventor, musician, and occasional triathlete. My current work and projects mainly involve audio technology for the web and iOS. I'm the author of AudioStretch, an audio time-stretching/pitch-shifting app for musicians. Past jobs have included writing speech recognition software for Apple, creating automatic video editing software for muvee, and designing ASICs for Nortel. I hold a Bachelor of Applied Science (Electrical Engineering) from Queen's University and a Master of Arts in Electroacoustic Music from Dartmouth College.
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Performance of Flash/AS3 vs. Silverlight/C#

  1. Tyler says:

    Thanks for posting your results. I guess I sort of expected this, but it’s still disappointing to see AS3 coming up so short. I’ve looked into Silverlight in the passed, but I really prefer Flash’s display list and drawing API’s.

    By the way, there’s some experimental functionality in the current Flash Incubator build for feeding mp3 data from a ByteArray into a Sound object for playback. It makes it much simpler to take an mp3 file selected by the user via FileReference and play it without fancy workarounds.

  2. Pingback: An FFT in C# | Gerry Beauregard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s