Tag Archive for 'Optimization'

Unnamed functions

Why you shouldn’t use them. What’s more, every function call costs time, think about inlining simple functions code.

Faster Math.ceil()

For those of you who optimize every bit of code.

va.lent.in — what a flash developer’s site should be

valentin

A couple of weeks ago I released my new personal portfolio/sandbox site — va.lent.in. I need more opinions and less bug reports.

I like projects which make me learn something. During this one I learned a lot…

Continue reading ‘va.lent.in — what a flash developer’s site should be’

as Class vs. Class(..)

Some of you might think that

p as Point

and

Point( p )

are equal. They might “mean” the same but they are not. First of all if p can’t be converted to Point result of the first operation is null, but the second one will fail with an error like this:

TypeError: Error #1034: Type Coercion failed: cannot convert 42 to flash.geom.Point.

What’s more, the first one is faster. Here’s the test:

public function typetest()
{
  var total: int;
  for ( var i: int = 0; i < 10; i++ )
    total += execute();
  trace( "as", total/10 );

  total = 0;
  for ( var i: int = 0; i < 10; i++ )
    total += execute2();
  trace( "cast", total/10 );
}

private function execute(): int
{
  var t: int = getTimer();
  var i: int = 0;
  var p: * = new Point();

  for ( i; i < 1000000; i++ )
  {
    p as Point;
  }
  return getTimer() - t;
}

private function execute2(): int
{
  var t: int = getTimer();
  var i: int = 0;
  var p: * = new Point();

  for ( i; i < 1000000; i++ )
  {
    Point(p);
  }
  return getTimer() - t;
}

This code outputs for me the following execution times:

as 157
cast 217.7

Speed vs. OOP

As I found out, AS3 compiler is too stupid to add even trivial optimizations.

I’ve been trying to optimize one of my heavy libraries I use everywhere for some time. It parses a lot of XML using e4x and creates many objects which it has to check using isSomething() function which is just 3 lines of code. So, this function is executed for every single object created.

Let’s check the test now.

var t: int;
var v: int = 5;
var w: int = 6;

for ( var i: int = 0; i < 5; i++ )
{
  t = getTimer();
  for ( var j: int = 0; j < 1000000; j++ )
  {
    var k: Boolean = func( v, w );
    //var k: Boolean = v > w;
  }
  trace( getTimer() - t );
}

private function func( v: int, w: int ): Boolean
{
  return v > w;
}

This executes on my Macbook Pro for 400ms.

If you uncomment the second line and comment the first one, execution time drops to 90-100ms. Which is 4 times less. But this is for 100000 calls, you can say. Remember that 30fps = 33ms for every frame, so one millisecond here and one millisecond there give noticeable result. And as I said the same short function is executed for every object. And there can be a lot of them. Though I don’t have to do it every frame but I don’t want my application freeze in the beginning either.

Moreover,

var t: int;
for ( var i: int = 0; i < 5; i++ )
{
  t = getTimer();
  for ( var j: int = 0; j < 1000000; j++ )
  {
    var k: int = value;
    //var k: int = _value;
  }
  trace( getTimer() - t );
}

public function get value(): int
{
  return _value;
}

it’s the same for getters and setters because they are also functions.

So I just replaced all calls of this function for actual code (6 occurrences). Every programmer who knows OOP will say that it’s bad to copy and paste code, if he doesn’t know why I did that. He might also go post on the forums that I am a noob, make a function of these pasted code snippets and will be proud of himself.

Here’s the question. Speed or OOP? While AS3 compiler is so dumb and can’t inline simple functions like this I’ll have to break readability in favor of execution speed.