Re: Case Construct
- Posted by CChris <christian.cuvier at ag?iculture.g?uv.fr> May 10, 2008
- 647 views
Matt Lewis wrote: > > Michael J. Sabal wrote: > > > > Forgive me for asking, but what benefit does case offer over elsif other > > than > > saving a handful of keystrokes? > > A C-style case statement also offers fall through: > }}} <eucode> > switch foo do > case bar: > -- do stuff > exit > case special_baz > -- do stuff, then continue with normal baz > > case baz > -- do baz stuff > exit > > default > -- do stuff > end switch > </eucode> {{{ > Matt Right. And, if e want, we can get it to process ranges, like in:
select(some_expr()) case -1,2: do_this() exit case 3 thru 6: do_preparations() default: the_real_stuff() end select
I think that all it saves, apart from the keystrokes, is parsing the various elsif statements. It can be implemented in the front end, but as usual without much of a performance gain. It was argued that a select statement winds up coded as a REP SCASB to get the branch address position in a table, then a JMP dword [EDI+some_offset]. Problem is that predicting these branches is difficult for the CPU, cache misses will be the rule, and they are more and more expensive as the pipeline of Intel CPUs lengthens. If correct (which I didn't assess by direct testing), then the select statement would have been useful with say the early Pentiums, but less so now. This said, I would vote for it if it can be implemented more efficiently. It makes code clearer, and the fall through is sometimes useful. CChris