What is this comparision related with or which is faster?


(Bora Kasap) #1

I’m wondering about a boolean comparision something like:

i have two different entities:

var a:Entity = FP.world.add(new Entity);
var b:Entity = FP.world.add(new Entity);

and another empty variable for setting a or b in future

var z:Entity = null;

then after z variable was set with random game events like “z = a or b” i want to check which is defined as z like,

if (z == a) trace("z == a");
else if (z == b) trace ("z == b");

that works without any problems. but i’m going to use that comparision in lots of entities like hundreds, because of that, i want to know which is faster for a comparision like that:

i’m curious about is this faster?

 if (z.name == a.name) trace("z == a");
 else if (z.name == b.name) trace ("z == b");

because of i read that names are unique for every instance?


(rostok) #2

I would say that z==b would be faster as it is simpler and will not dive into String comparison or object property (name). However this approach seems a bit awkward. Where is the benefit in finding two variables being exactly the same one? You already have entity’s the reference in z.


(Jacob Albano) #3

name defaults to null, and it’s entirely possible to give multiple entities the same name, so you’re not guaranteed that every instance will have a unique name.

On a more general note, comparing by reference is always faster than by string. When you do a string comparison, this is basically what’s going on:

if (str1.length != str2.length) return false;

var len:int = str1.length;
for (var i:int = 0; i < len; ++i)
{
    if (str1.charAt(i) != str2.charAt(i)) return false;
}

return true;

A highly simplified approximation, but you get the idea. If the two strings are identical, the loop will run all the way to the end. Multiply that by the hundreds of checks that you’re talking about and you’re in for some trouble.

On the other hand, references are stored in the flash player as pointers, which are simple integers that refer to addresses in memory. Under the hood, your entities are being stored like this:

z : 0x3A28213A
a : 0x6339392C
b : 0x7363682E

Comparing integers is dead fast. Go with reference comparisons.


(Bora Kasap) #4

Cool, thanks!

i need to remove some specific entities from an array and that’s the way i found

private var _targets:Array = new Array; //that array isn't empty when i call "removeTarget()" function below


public function removeTarget(comp:Entity):void
		{
			var newTargets:Array = new Array();
			for each (var oldComp:Entity in _targets)
			{
				if (oldComp != comp) newTargets.push(oldComp);
			}
			_targets = newTargets; //new array created without pushing removed one
		}

That would be cool if you guys know how to do that faster? :confused:


(Jacob Albano) #5
FP.remove(_targets, entityToRemove);

(Bora Kasap) #6

Oh yeah! <3 <3 <3 !!!


(rostok) #7

I know you guys tend to shun early optimizations yet getting into FP.remove() will show that there is array.indexOf and layer array.splice() both which would take some time in case there are lot of elements. Alternative would be to store reference to the array both with index in each entity that needs to be removed. In case of removal instead of splice do sth like this:

array[index] = array[array.length-1];
array.pop();

But this optimization should be used only if removal is a real bottleneck.


(azrafe7) #8

There’s also World.removeList() which should be pretty fast.


(Bora Kasap) #9

i’m already using shift() and pop() functions but i think i understood what you mean. actually no need an array reference for this? right? i think storing index of the entitty in the entity which is pushed into array. then no need for indexOf() search process, i can implement FP.remove(object) to FP.remove(index);


(Bora Kasap) #10

But if i do that, that’llbe too complicated because of entities mostly in multiple arrays. Thanks for answers. I think i don’t need more help about that. FP.remove() is the best for my situation.