Which is faster?

Miscellaneous Forums/General Discussion/Which is faster?

A seemingly inane problem has come to my attention, but I can't possible test the outcome.

Which bit of code do you think is faster:-

if moo =< 10 then

if moo < 11 then


I would say the second one, because the '=<' argument has to perform two calculations. Surely the same as going:-

if moo = 10 or moo < 10 then


But if it's handed logically like a set of 'if' statements in memory in the joint operand then if it checks for one argument and it is true it will surely ommit the second instruction.

Anyone know the answer, I'm wondering whether spent the rest of my life avoiding =< and => commands.

I tend to use option2 out of habbit. As you say it should be faster but the speed difference is probably irrelevant with modern processors.

X86 has an instruction for jump less than or equal, so theoretically (assuming the instructions execute in the same amount of cycles) there'd be no difference.

Any half decent optimizing compiler could convert one to the other anyway for platforms without a JLE opcode, because you're just testing a variable against a constant. If you turned 10 into another variable, it may be a little different.

Without doing a simple speed test of 5 lines in length my immediate thought is one logic gate is much the same as another - it just performs a different task. The results may be processor dependant.

Loop the calculation over a few thousand times and time it then you'll know for sure.

You've been eating that mature cheddar again lol

I wouldn't worry about it but if you really must know then do a loop calculation like Becky says or I'll send the cheese police round to beat you with parmesan truncheons.

it doesnt matter,the speed difference is too small to notice

If a logic gate is your primary performance bottleneck, stop coding. You're done. Ship it. :)

LOl @ mature cheddar :P

Well I think it's an important issue. I'm going to avoid using them where possible, certain for ints anyway.

If BBasic is compiled to assembly properly then it is 100% the same due to both being assembly commands with the same number of cycles. However, if it DOESN'T then who knows, testing is the only answer in a big FAT loop.

I would say the same thing .. there are probably CPU instructions that are specific to the kind of test being performed, such as the old BLT (Branch if Less Than) and BLE (Branch if Less than or Equal) on the Motorola 68000 line of cpu's. I especially would be surprised if the <= ever uses two instructions instead of one.

Yeah, the x86 equivs are JL and JLE. I used to 68000 too. btw AngelDaniel a tip, don't think me rude but it's CPUs no apostrophe, trust me.

Jump if Lower (JL) and Jump if Lower or Equal (JLE) require the same number of cycles, although it matters if they actually jump or not. On a 486 it takes 1 cylce if it won't jump and 3 cycles if it will.

Considering we have a 66 MHz CPU the diffrence (between jumping and not jumping, after comparing) is 0.0000030303 ms. Not a big deal. Especially when you're on a 3GHz machine.

I couldn't find how many cycles it took in my assembly book, glad someone could.