Does PB short-circuit expressions?

Everything else that doesn't fall into one of the other PB categories.
matthew180
User
User
Posts: 64
Joined: Mon Jun 30, 2003 5:36 pm
Location: Michigan
Contact:

Post by matthew180 »

tinman wrote:Just that short circuit evaluation in itself isn't a bug or problem, but it does seem to be broken at the moment.

Broken how? It's working fine for me and worked as expected in the little test app I wrote.

Please explaine how it is not working. Also, is short-circuiting something that Fred is only considering? My vote is to keep it, especially since PB already has it and changing to non-short-circuiting would break a lot of code (at least for me it would.)

Thanks,
Matthew
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

matthew180 wrote:
tinman wrote:Just that short circuit evaluation in itself isn't a bug or problem, but it does seem to be broken at the moment.

Broken how? It's working fine for me and worked as expected in the little test app I wrote.

Please explaine how it is not working.
Dare2 or Kale (sorry guys, I fogot) posted a thread about combining logical operators. The short circuit evaluation misses out the entire line, ignoring things it shouldn't. I can't remember which forum it was in though and can't be bothered looking for it :)
Also, is short-circuiting something that Fred is only considering? My vote is to keep it, especially since PB already has it and changing to non-short-circuiting would break a lot of code (at least for me it would.)
It's there and AFAIK (for what it's worth) it's staying.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
guido
User
User
Posts: 14
Joined: Tue Nov 25, 2003 4:56 pm
Location: Old Europe - Germany

Post by guido »

tinman wrote: Short circuit evaluation is a good thing. It means you don't need to evaluate both sides of a logical expression to get the result. Therefore if you have a simple comparison logically combined with an evil complex one, you're best putting the simple one on the left because it may mean you can avoid carrying out the right hand side thus saving you time.
For some background look here.
I see it a little differently. The most important thing is to know, whether it happens or not unconditionally, or whether it can be controlled by a compiler switch (as in good old Turbo Pascal). Many compilers activate it in in higher optimisation levels only.

In the time of simple CPUs it was a classical time/space trade off: querying the boolean intermediate result and jumping out of the comparison chain, if the result can't depend on terms to follow, adds some opcodes and saves some time. In time of more advanced CPUs jumping flushed the prefetch queues, so it was not paying off always for short jumps (therefore ARM conditional execution). Now with branch prediction and caches it again may pay off, but its difficult to judge without benchmark.

Summary: Short circuiting saves execution time, if there are MANY terms or if some of the terms are time-consuming functions. It leads to grey hairs, if one of the functions has a side effect leading to sporadic errors. Even if not supported by the compiler, it can be substituted by explicit coding.
guido
Post Reply