Working on huge projects sometimes let you take long optimization sessions.
During one of these sessions I was trying to find a way to optimize a bunch of code which was taking too long time to process.
The big big bottleneck I discovered, and I didn’t know before (my fault), was the **[Bindable]** attribute of one of the classes included in the process.
This process was creating something like 1 million instances of this class during its life. After a first optimization in which I created an object pooling, thus reducing the total execution time from 16 seconds to 13, I found this incredible trick which reduced the total execution time to **1 second!!**
Let’s say you have a class like this:
package it.sepy.test
{
[Bindable]
public class RGB
{
private var _blue: int;
private var _green: int;
private var _red: int;
public function RGB()
{
}
public function get blue(): int
{
return _blue;
}
public function set blue( v: int ): void
{
_blue = v;
}
public function get green(): int
{
return _green;
}
public function set green( v: int ): void
{
_green = v;
}
public function get red(): int
{
return _red;
}
public function set red( v: int ): void
{
_red = v;
}
}
}
and a simple script which create 100000 instances of this class (my script wasn’t like this, but for simplicity I’m doing this example):
package
{
import flash.display.Sprite;
import flash.system.System;
import it.sepy.test.Pool;
import it.sepy.test.RGB;
public class test extends Sprite
{
public function test()
{
startTest();
}
private function startTest(): void
{
var t1: Number = new Date().getTime();
for ( var i: int = 0; i < 100000; i++ )
{
var rgb: RGB = new RGB();
rgb.red = i % 255;
rgb.green = i % 255;
rgb.blue = i % 255;
}
var t2: Number = new Date().getTime();
trace( 'total execution time:', ( t2 - t1 ), 'ms' );
trace( 'memory usage:', System.totalMemory / 1024, 'kb' );
}
}
}
now launch the script and in the output I have this result
total execution time: 2381 ms
memory usage: 5924 kb
NOW. Remove the **[Bindable]** property from the RGB class and launch again the test script. See at the output console and the new results are:
total execution time: 236 ms
memory usage: 2720 kb
So, I know that bindable were not created for this kind of jobs, but if using it make 10 time slower my scripts, then I don't see any valid reason to use it anywhere.
Share with...