Math.abs()…where must be a faster way
by Administrator on Sep.24, 2010, under flash performance
Math.abs() is handy to use…but it´s quite slow. Here is a significant faster alternative (about one-third faster):
var test:Number = 0.0;
var sum:Number = 0.0;
var i:int;
var duration1:int = 0;
var duration2:int = 0;
var diffPercent:Number = 0.0;
var loopCount:int = 5000000;
var startTime:int;
startTime = getTimer();
for (i = 0; i < loopCount; ++i) {
test = i * -1;
sum += Math.abs(test);
}
duration1 = getTimer() - startTime;
trace(duration1 + " - " + sum);
sum = 0.0;
startTime = getTimer();
for (i = 0; i < loopCount; ++i) {
test = i * -1;
sum = (test < 0) ? sum - test: sum + test;
}
duration2 = getTimer() - startTime;
diffPercent = Math.round(duration2 / duration1 * 10000) / 100;
trace(duration2 + "(" + diffPercent + "%) - " + sum);
Results:
926 - 12499997500000
581(62.74%) - 12499997500000
Any ideas to further improve that?
fov : shadow cast implementation
by Administrator on Jan.08, 2010, under flash performance, graphics
Knowing the field of view (fov) of an object, provides some interesting use cases. Determination of what is in sight of a character, lightening…and other ideas. There are some brilliant algorithms for bitmap based calculations out there: BASIC, Digital, Permissive (several variations) and ShadowCasting. I decided to implement the ShadowCast algorithm in as3, due to the overall performance in indoor and outdoor environments.
Here is a simple example, to show the functionality (click for interaction):
My implementation is very specialized and contains a lot of redundant code. Initially, I planned to create a flexible implementation, detached from bitmaps and visibility rules, handled by a given object. Mainly responsible for that is the fact, that function calls are relatively expensive.
Do you have any ideas to further improve the performance?
Here ist the source:
ShadowCaster.as
static functions
by Administrator on Jan.03, 2010, under flash performance
Static function calls are slower than normal ones. On the other side, creating a new instance is a very expensive operation. But when you call a function several times in a loop, it pays off using it in a non static way.
For a small test, I created this simple class with a normal and a static function.
public class CallMe { public function CallMe() {} public function doSomething(number:int):int { return ++number; } public static function doSomethingStatic(number:int):int { return ++number; } }
Tested with this simple test code…
private var _iterations:int = 1000000; private function startTest():void { var test:int = 0; var normalStartTime:int = getTimer(); var testObj:CallMe = new CallMe(); for (var i:int = 0; i < _iterations; ++i) { test = testObj.doSomething(test); } trace("Normal call: " + (getTimer() - normalStartTime)); test = 0; var staticStartTime:int = getTimer(); for (var j:int = 0; j < _iterations; ++j) { test = CallMe.doSomethingStatic(test); } trace("Static call: " + (getTimer() - staticStartTime)); }
…comes to this result:
Normal call: 352
Static call : 422
expensive function calls
by Wurzel on Jan.03, 2010, under flash performance
While optimizing an implementation of the shadow cast algorithm, I realised that function calls are really expensive in terms of performance. Look at this small example:
private var _iterations:int = 1000000; private function startTest():void { var inlineStartTime:int = getTimer(); for (var i:int = 0; i < _iterations; ++i) { 10 + 10; // just to use CPU time } trace("Inline : " + (getTimer() - inlineStartTime)); var externalStartTime:int = getTimer(); for (var j:int = 0; j < _iterations; ++j) { doSomething(); } trace("External: " + (getTimer() - externalStartTime)); } private function doSomething():void { 10 + 10; }
That comes to this result:
Inline: 94
External: 382
Thus, simple functions should be avoided in loops with many iterations if possible. Inline the statements instead.