Re: "case" or better? (2.6 suggestion mode)
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Apr 03, 2005
- 489 views
On Sat, 02 Apr 2005 23:09:48 -0800, Al Getz <guest at RapidEuphoria.com> wrote: >Very interesting. Can i ask why you want to have multiple if's >process the same message? I'm not sure I understand your question. I suppose I could have written:
procedure Cancel() FaceIdx=InitFaceIdx PointIdx=InitPointIdx applyFont() end procedure function Close() isFaceName = faces[FaceIdx] if FaceIdx>=5 then isPointSize = sizes[FaceIdx][PointIdx] end if setVisible(PFwin,False) return 1 end function ... if msg = WM_CHAR and wParam = VK_ESCAPE then Cancel() return Close() elsif (msg = WM_CHAR and wParam = VK_RETURN) or msg = WM_COMMAND then if id = PFcncl then Cancel() end if return Close() elsif msg = WM_CLOSE then return Close() end if return 0
Is that what you meant? >I think the argument *for* the 'case' statement is to have a single test >determine >where to jump in the program so you dont have to wait for 100 elsif's >to fail before you get to the 101'th elsif. Any gain is only possible if you restrict the proposed case statement to dealing with a range of n positive integers 1..n, where n must be known at compile-time. Otherwise, in terms of performance, a case statement is doomed to be no faster than a huge if/elsif statement, especially if you want things like unbounded ranges (eg x>2000) or default clauses, or be able to deal with things like windows messages, or the dynamically-generated win32lib control-ids, even if they are declared as "constants". Theoretically, it is not difficult to optimise away the repeated loads if consecutive elsif clauses have the same lhs, although I doubt Rob currently does this. I suspect you are imagining a performance gain which is not going to materialise. Regards, Pete