Re: Minimum & Maximum
- Posted by jiri babor <jbabor at PARADISE.NET.NZ> Sep 25, 1998
- 553 views
Michael Palffy wrote: >How's this.. > >function max(sequence a, sequence b) > return a *(a>=b) + b *(b>a) > end function Now we are really cooking, echos of c.r.white... First of all, just a minor thing, Mike, you probably want to change the parameter type from a sequence to an object, to cover plain atoms as well as sequences. So your min and max functions become function min(object a, object b) return a*(a<=b) + b*(b<a) end function function max(object a, object b) return a*(a>=b) + b*(b>a) end function Nice and short, and it works! (I does not happen all that often on this list ) But (there is almost always a 'but' lurking somewhere in everything!) these function have no explicit type checking that all the previous suggestions had. So it gives me a chance to ask a question I wanted to ask anyway: do we really need those explicit checks? And I think, we do not! The compiler itself will eventually tell us, anyway, if we try something stupid, illegal. The error message may be a bit more cryptic ('an attempt to subscribe an atom', or whatever...), but we will not get away with it. And these superfluous checks always slow things down, sometimes considerably! Now a treat for the benchmark freaks. I stripped my minimum function down to function minimum(object a, object b) if atom(a) then if b<a then a=b end if else for i=1 to length(a) do a[i]=minimum(a[i],b[i]) end for end if return a end function to make it more directly comparable with Mike's. The times are in seconds for 1,000,000 iterations on my 233 MHz Pentium II, 64 Mb ram, under Win95, the same simple set as in my previous post, tick_rate(1000): Mike's Mine ================================================================ min(5,3) 1.14 0.91 min({5,3,1},{-5,2,6}) 6.69 6.89 min({5,3,{4,5},1},{-5,2,{-7,7},6}) 11.82 13.27 Not much in it! jiri