Qbasicnews.com

Full Version: code speed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
what is faster in general computing regardless of language(if that works)
Code:
If X=16 or X=1 or Y=1 or Y=8
CALL redrawmap
or

Code:
If X=16
CALL redrawmap
if X=1
CALL redrawmap
if Y=1
CALL redrawmap
if Y=8
CALL redrawmap

the purpose of the code is to redraw the map if x or y is equal to those given points, it is testing the sprite position. points (1,8 ) , (16,1) ... do not occure
Depends on the language, some languages can break out of conditional tests when it encounters the first false. So it would be like your second code.

Although, the second code may be slower, as it holds more instructions (I think)


But it really depends on the language...


In theory though, your second code is the most efficient way of doing it, less checks
As the Z!re already said in previous post, it depends on the compiler you have.

You have a chance the compiler compiles it to the same series of cmp and jmp commands (or similar), thus resulting in exactly the same run speed.
It is however possible too that if you have an efficient compiler the first code will be compiled to faster code (the second contains more checks).
Yup, just a matter of lazy evaluation.

Lazy evaluation means that once you have proved that the expression is whether false or true you stop evaluating.

For example, if you have an OR. If any of the operands used in the OR expression is true, the expression will be true and there's no need to keep evaluating. If you have:

Code:
IF x = 3 OR y = 2 OR z = 8 OR w = 10 THEN ...

And x = 7, y = 3, z = 8 and w = 10. A lazy evaluator would say:

¿is x = 7? No, keep looking.
¿is y = 2? No, keep looking.
¿is z = 8? YES: the expresion equals true, execute code block.

If the evaluator is not "lazy" (don't know the word in English, sorry), it will keep evaluating even when it's not necessary, i.e. it will check if w = 10.

Same happens to AND:

Code:
IF one = 10 AND two = 10 AND three = 10 THEN ...

With one = 0, two = 7 and three = 10. A lazy evaluator would say:

¿is one = 10? NO: the expresion equals false, don't execute code block.

Get it?
The language is TI-83 BASIC, dont know if that helps.
Quote:If the evaluator is not "lazy" (don't know the word in English, sorry), it will keep evaluating even when it's not necessary, i.e. it will check if w = 10.
so the second code is better?
I don't know which's faster but if you got a stopwatch you could
create a test program to check which one's faster.

Since it's a program for a calculator (-> with little memory)
you might perhaps also want to check if there is size differences...
By the way, in the second piece of code, it would be better to use ELSEIFs. That way, as soon as one of the conditions is satisfied, the consequent IF statements don't need to be evaluated either.

This would probably make very, very little impact on the speed of your program, by the way... micro-optimisation isn't really needed when we have processors the speed they are now. Smile Unless you're writing this for a slower processor. Wink

-shiftLynx
Quote:Unless you're writing this for a slower processor. Wink
Or when you have billions of those commands per second, and then microoptimization can get a huge advantage.

There's always two or more ways of logical deduction Wink
it is a 6Mhz processer. there is no elseif statement in this basic version
Quote:The language is TI-83 BASIC, dont know if that helps.
Quote:If the evaluator is not "lazy" (don't know the word in English, sorry), it will keep evaluating even when it's not necessary, i.e. it will check if w = 10.
so the second code is better?

No. You haven't got it. I wasn't comparing both pieces of code, I was comparing two ways of evaluating expressions applied to the first piece. Both pieces of code are interpreted the same or differently depending on how the compiler evaluates expresions.

If the compiler has a lazy expresion evaluator, the first code is better as less checks are done in an average case.

If the compiler hasn't a lazy expresion evaluator, both codes are as fast: the same amount of checks are done.

Adding ELSEIF to the second constructor (as suggested) would be a way to emulate lazy expresion evaluation for OR in an environment that doesn't have it, which ain't the case.[/img]
Pages: 1 2