Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > lazy evaluation (of conditions)

lazy evaluation (of conditions)
Thread Tools
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Nov 11, 2001, 06:00 PM
 
hi,

does anybody know if there is a way i can force obj-c to do lazy evaluation?
if have an if(...) with over 20 conditions in it which has to be evaluated hundreds of times which makes it kind of slow.
i already rewrote everything to be !(!a && !b) instead of (a || b), but i don't feel any speed improvements...

any ideas?
     
Fresh-Faced Recruit
Join Date: Jun 2001
Location: Pleasant Valley, NY
Status: Offline
Reply With Quote
Nov 11, 2001, 10:30 PM
 
I think what you are referring to is "short-circuit evaluation" which is a bit different than the lazy evaluation found in some functional programming languages. If I am correct, you want "if (a || b)" to only evaluate b if a is false (if a is true, the boolean statement is true). If I am mistaken about what you are asking, I apologize.
What you are asking is standard C. Both || and && are short-circuited. That may be why you don't see a speed change by switching between the two. The best advice I can give use is to make sure the simplest components are evaluated first, if that is possible.

Matt
     
seb2  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Nov 12, 2001, 06:14 AM
 
that is what i meant. sorry for my terminology, didn't know it was "short-circuit evaluation" in english. ;-)

there should be a difference between the || and && though because in most cases || has to evaluate more.

i hope i found another way of doing things by chance.

thanks a lot for your help, though!
     
Mac Enthusiast
Join Date: Sep 2001
Location: McKinney, TX
Status: Offline
Reply With Quote
Nov 12, 2001, 09:33 AM
 
Originally posted by seb2:
<STRONG>there should be a difference between the &#0124;&#0124; and && though because in most cases &#0124;&#0124; has to evaluate more.</STRONG>
I don't know where you came up with that impression. Let's compare (a&&b) and (a&#0124;&#0124;b). If "a" is 1, (a&#0124;&#0124;b) only has to evaluate "a", but (a&&b) has to additionally evaluate "b". If "a" is 0, the inverse is true: (a&#0124;&#0124;b) has to additionally evaluate "b" whereas (a&&b) does not. So, the only time &#0124;&#0124; has to evaluate more is if the "a" condition is 0 more often than it is 1.

[ 11-12-2001: Message edited by: TheBum ]
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Nov 12, 2001, 12:50 PM
 
It depends on what values are comonly being passed to it. &#0124;&#0124; stops when it reaches a true, && stops when it reaches a false. I'm sure you've seen this before:

!(a&&b) == !a &#0124;&#0124; !b
!(A&#0124;&#0124;B) == !A && !B

I forgot the name of the guy who made these... was it Demov or something? So both are equivalent if you code it right.

Here's an example in some code. Say I want to know if a user has clicked his mouse and is wearing a green hat but the user rarely wears a green hat. So I'll use the && operator. If I ask for the green hat then the mousedown, my code will evaluate slightly faster on average, because &&'s stop at falses (which greenhat usually is). If I did it the other way, then I'd be checking if the mouse was down, which happens more often than the user is wearing a green hat, so both conditions are going to need checking more often.

I hope that made some sense. With 20 conditionals, you may want to see if there's anyway to condense your checking. 20 is kind of a lot, but not unheard of.

HTH,
F-bacher
     
Fresh-Faced Recruit
Join Date: Jun 2001
Location: Pleasant Valley, NY
Status: Offline
Reply With Quote
Nov 12, 2001, 02:21 PM
 
Ghoser777,
One important assumption you make in your example is that checking for a green hat takes approximately the same execution time as checking for a mouse down. Imagine if checking for a green hat takes considerably longer to check - then it should obviously be evaluated second. This is why I always tend to order conditionals with the cheapest (in terms of time) first. If two checks will take the same time, then I'd worry about putting whichever is more likely to short-circuit the condition first.

Matt
     
Professional Poster
Join Date: Dec 2000
Location: Chicago, Illinois
Status: Offline
Reply With Quote
Nov 12, 2001, 04:20 PM
 
Yeah yeah yeah, I just didn't get into that much detail. I was only thinking of checking booleans. You're right, check the one that takes the least amoun of time, but when times are equal, check the ones that are more likely to stop the evaluation process first.

F-bacher
     
seb2  (op)
Grizzled Veteran
Join Date: Feb 2001
Location: Germany
Status: Offline
Reply With Quote
Nov 12, 2001, 05:56 PM
 
ghoser777, couldn't have explained that better.

all the evaluations take the same time, so i ordered them according to likelihood of being true. and, well, those 20 are necessary -- unfortunately. i want to filter out contents of packages and so i have to check whether the path components end in ".app", ".bundle",...
i know that nsworkspace provides a method to check that, but a) it takes longer than comparing paths manually because it accesses the hd every time and b) it'll actually return true only if the path is a reference to a package, not if it references a file *in* a package, so i'd end up breaking up the path into components anyway...

[edit: slightly improved english :-)]

[ 11-12-2001: Message edited by: seb2 ]
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Dec 8, 2001, 03:19 AM
 
Well, this might be a silly suggestion, but why don't you create a dictionary that has as its keys the strings that you want to filter? You can use NSDictionary or the equivalent core foundation object. Then, use the url-handling routines to get the extension of the filename you want to check and finally, try to get the dictionary entry that has that file extension as its key. This way, you've got the pre-optimized dictionary hash routines working for you. If you get a non-nil reply back, you've matched one of your filter types. If you get a nil, you don't need to filter.

Of course, this assumes that the key-matching hash is optimized.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:51 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2