"not less than" versus "greater or equal to" speed

3.9k views Asked by At

I was wondering if someone knew or has one of those accurate speed tests could test "not less than" versus "greater or equal to" speed in Actionscript 3 (Air 2.5 if that makes a difference)?

I have virtual machines running on this computer and I am getting very inaccurate results.

Using it as such

if ( !(__index < _vector.length) ) return;

or

if ( __index >= _vector.length ) return;

I would have thought the first one, since all its doing is 1 test and then reversing it, but actionscript 3 has some of those quirks where you can never be sure.

3

There are 3 answers

1
Jeff Walden On BEST ANSWER

There shouldn't be a difference in speed, in theory. ActionScript uses the nanojit JIT library to compile code, and I know for a fact (having worked on that code before in Mozilla's JavaScript engine, which shares nanojit with Adobe's ActionScript) that the code to implement comparisons will transform simple conversions like inversion of a comparison into the inverse comparison. So in theory the only difference is another cycle or two spent to compile the code. This is not worth worrying about.

On the other hand, modern CPUs are complex beasts, and infinitesimal perturbations can make notable differences. So while I'd lay strong odds on there being no difference between the two, I wouldn't bet the farm on it.

1
regex On

Your question concerns computer science more than it does action script specifically as most languages will attempt to compile to the most optimal machine code as possible.

So, I will use a C++ example to answer your question.

int j = 16;
if (!(j < 10))
{
    int l = 3;
}

if (j >= 10)
{
    int l = 3;
}

This produces the following key section in assembly:

00231375  cmp         dword ptr [j],0Ah  
00231379  jl          wmain+32h (231382h)  
0023137B  mov         dword ptr [l],3  
00231382  cmp         dword ptr [j],0Ah  
00231386  jl          wmain+3Fh (23138Fh)  
00231388  mov         dword ptr [l],3  
0023138F  xor         eax,eax  

Lines 00231375 and 00231382 are your actual tests contained in the if statement. As you can see, both my < and >= tests were compiled as the same identical code in assembly (when comparing two integers). Therefore, either test will take the same amount of time on the CPU since they both result in the same test (if left < right, skip if block). This will most likely be the case with the action script compiler.

However, one question could be if the JIT compiler takes longer to compile !([int] < [int]) or [int] >= [int]. My guess is that the difference is probably not enough to matter.

1
Lucas Speranza On

make a loop and use getTimer() to discover. Try something like this:

var startTime:int = getTimer()
var count:int = 1000000
for (var i:int = 0;i<count;i++) {

    if ( !(__index < _vector.length) ){

    }
}
trace("The first script took "+String(getTimer()-startTime) + "ms to run")



startTime = getTimer()
for (i = 0;i<count;i++) {

    if ( __index <= _vector.length ){

    }
}
trace("The second script took "+String(getTimer()-startTime) + "ms to run")

Also, if you get innacurate results with this tecnique, try making the count variable bigger. Removing the "_vector.lenght" verification from the loop may be usefull if you just need to check the ">" and ">=" performance