FacebookTwitterGoogle+Share

function race 001!

One day for fun, Karl and I thought up few different ways to accomplish the same thing. That sort of thing is fun.

No, it’s fun.

I decided to compare two of them for speed. The range and domain of both functions are {1,2,3}.

Method A
Based on a table.

function( a:int, b:int ) : int {
	var c:int = a^b;
	return c ? c : a;
};

Mathod B
Based on the same table, but karnaugh-mapped, circuit-design style.

function( a:int, b:int ) : int { 
	var a0:int = a&1;
	var a1:int = (a&2)>>1;
	var b0:int = b&1;
	var b1:int = (b&2)>>1;
	return ((( a0&~b0 | (~a0)&b0 | a1&b1&b0 )<<1 ) | ( b1&~a1 | a1&~b1 | a1&a0&b0 | (~b0)&~a0 ))&3;
};

Function racer

The number below each method is the number of milliseconds consumed for the total operations performed (above the methods).
The operation size (at the top) is scaled to keep the framerate between 29 and 32.

Click on it to start the race. Click off of it to stop the race.

Method A is always faster than Method B (except sometimes in firefox for some reason) on my computer, but the difference is minimal (like .5%). There are obviously little ways in which we can tweak the algorithms. For example: we could not declare a0, a1, b0, and b1 every single time.

 

Comments

  1. shaun says:

    Ops Performed: 40388245
    Method A: 25572
    Method B: 25244
    Ops Per Frame: 17227

    This is on my work computer. P4 3ghz. Strange that it outperforms my home machine, An Athlon X2 3800+ which got around 15k ops per frame. Also Method B is ~1% faster than Method A on this computer? Running Chrome. I had a lot of stuff open at the same time, so that might be goofing the results.

  2. Brad says:

    Ops performed: 40130036
    Method A: 9968
    Method B: 9989
    Ops per frame: 56652

    That is weird!
    I tried to do about the same number of operations as you, for reasons of matching. I got Method B as ~1 slower than A this time.

    LET’S RACE EVERYTHING FROM NOW ON!

  3. k says:

    Ops Performed: 20126084
    Method A: 15130
    Method B: 17064
    Ops Per Frame: 14262
    Flash Player: 10.0.22.87 (debug)
    Browser: Chrome 3.0.195.21

    My computer has a lot of pagefaults, so speed tests are less than ideal on it. Also I’m running the debug player >_>

    I wonder if the strangeness in your speeds shaun is from player version differences?

    It’s too bad that actionscript can’t compile-on-the-fly like javascript. If it could we could make an interface to write in and try whatever function you want.

  4. Brad says:

    that would be cool! Especially since it just defines the function and loops through an array like this:

    var methods:Vector.<Notepad> = new Vector.<Notepad>(2);
    methods[0] = new Notepad();
    methods[1] = new Notepad();
    methods[0].time_mc = a_time;
    methods[0].calc = function( a:int, b:int ) : int {
    var c:int = a^b;
    return c ? c : a;
    };

  5. k says:

    Could compile with a server script, or port asc to actionscript =S
    Or compile java with alchemy and run asc in actionscript 0.o

    I thought I’d download flash player 10 update 3 and try it out again but the download is big.

You must be logged in to post a comment.