Re: type string
- Posted by Roderick Jackson <rjackson at CSIWEB.COM> Mar 18, 1999
- 460 views
Ralph, I don't think short-circuiting is always necessarily faster than if..elsif in a language. For example, consider a section of code, which takes advantage of short-circuiting: if (expr1 or expr2) then code end if and compare it to: if (expr1) then code elsif (expr2) then code end if Obviously the second example introduces redundancy. But does it have to be slower even in theory? I admit that the first example should be faster than it would be if it didn't have short-circuiting, just because when the first expression is true, the second one isn't evaluated. Now, you suggest example #2 should be slower because it has a statement jump (to the evaluation code for the second expression.) But doesn't that same statement jump exist when short-circuiting is implemented? In order to not evaluate both expressions, a jump must be placed around the second evaluation whenever the first returns true. So in either case, if expr1 is true, a jump (at least in the tokenized, atomic form) must appear. HOWEVER... I just had to test this. I wrote a quick program that loops through example 1 about three million times, then example 2, then example 2 again, then example 1 again (to make sure there were no advantages to being run first). Taking the first pair (expr. 1 & 2) and the second (expr. 2 & 1), expression #2 was consistently *slower*. This was when expr1 and expr2 were simple integer variables T and F, containing a 1 and a 0 respectively. Changing those variables to real expressions had a curious impact.Replacing T with (T = 1) and F with (F = 1) reversed the results. Now the second example was consistently *faster*; in fact, the speed change was hardly noteworthy. Example #1, on the other hand, had enough of a speed decrease to make it the undesirable choice; about a 50% drop. (All of this was done under version 2.1.) I'm guessing that with the first run, since only variables were used, short-circuiting wasn't *really* implemented... performing a logical 'or' on the two integer values wouldn't have had a jump at all, and would be possible since the truth values didn't need to be seperately computed. Of course, once each expression needed to compute a truth value, a jump was required. (Why example 1 would be SLOWER than example 2 under that condition, I still don't know; I would have expected similar speeds.) BTW - I ran both types of tests under 2.0 as well. When simple integer variables were used, example 1 was still faster, which is what prompted me to think there was only a simple calculation being performed. When using (T = 1) and (F = 1) as expressions, example 2 won without ANY difficulty. Rod Jackson ---------- From: Ralf Nieuwenhuijsen[SMTP:nieuwen at XS4ALL.NL] Sent: Thursday, March 18, 1999 10:05 AM To: EUPHORIA at LISTSERV.MUOHIO.EDU Subject: Re: type string > Incidentally, I've found myself using the all-on-one-line-if for types. > Usually only one condition can be checked at a time if we want speed > and/or no-error evaluation. Euphoria Version 2.1 Listed as one of the features: Short Circuiting A statement jump, is still a statement jump and thus seperating such an in-statement will cause a little tiny bit of time. Something when measured should be noticable. Ralf