I’m always interested in performance comparisons. In the RIA world, two that you may have seen are Bubblemark and more recently GUIMark. Those tests look at graphics; I thought it would be fun to look at some tight code in a loop.

You can try the test here.

The code is below. If I’ve done something that tilts the test in favour of one or the other runtime, let me know.

Please don’t take this too seriously. I may have messed up the test; it is only one small aspect of performance; and there are lots of other factors to think about. I just find this sort of thing interesting.

ActionScript code:

public function countprimes():void {
var n:int = int(testnum.text);
var start: Date = new Date;
var i: int;
var j: int;
var numprimes: int;
var limit: Number;
numprimes = 1; // 2 is prime

for (i = 3; i<= n; i+=2)
{
var isPrime: Boolean = true;

limit = Math.ceil(Math.sqrt(i)) + 1;

for (j = 3; j < limit; j+=2)
{
if (i % j == 0)
{
isPrime = false;
break;
}

if (isPrime != true)
{
continue;
}
}

if (isPrime)
{
numprimes ++;
}
}

var end: Date = new Date;

var timetaken:Number = Number(int(end) - int(start));
timetaken = timetaken / 1000;

lbresult.text = "Number of primes up to: " + n + " is " + numprimes + ", counted in " + timetaken + " secs.";
}

C# Code:

void btnCount_Click(object sender, RoutedEventArgs e)
{
int n = Int32.Parse(this.txtMaxNum.Text);
DateTime start = DateTime.Now;
int i;
int j;
int numprimes;
double limit;
numprimes = 1; //2 is prime

for (i = 3; i<= n; i+=2)
{
bool isPrime = true;

limit = Math.Ceiling(Math.Sqrt(i)) + 1;

for (j = 3; j < limit; j+=2)
{
if (i % j == 0)
{
isPrime = false;
break;
}

if (isPrime != true)
{
continue;
}
}

if (isPrime)
{
numprimes ++;
}
}

DateTime end = DateTime.Now;

Double timetaken = end.Ticks - start.Ticks;
timetaken = timetaken / 10000000;

this.lbMessage.Text = "Number of primes up to: " + n + " is " + numprimes + ", counted in " + timetaken + " secs.";
}