From: Nicolas C. <war...@fr...> - 2003-05-29 02:11:00
|
> Enum.fold has to set up an exception stack every time it calls next. It > only catches an exception once- but it has to set up the stack frame every > single time. And that costs. You're benchmark is just comparing one try...catch with one None/Some matching. When you are doing some O(1) functional operations you have to do 1 try...catch and 'N' None/Some matching. But according to your results I modified the implementation of fold in order not to setup a stack frame every time : let fold f init t = let acc = ref init in let rec loop() = acc := f (t.next()) !acc; loop() in try loop() with No_more_elements -> !acc It should now be very efficient. > In either case, interface harmonization is a more important issue IMHO. > The exported definition of next should not be any more complicated than: > let next t = t.next () I already told several times about this : if we let escape the No_more_elements exception outside of the API, then maybe one iter upper in the call stack will catch it and break iteration , so if the user fail to catch it properly when calling next() it can cause very error prone behaviors since he won't be noticed about the exception being raised. exemple : let e1 = List.enum [1;2;3] let e2 = List.enum [] let combine x = (x , Enum.next e2) let l = Enum.of_list (Enum.map combine e1) => [] So we have to keep the current next implementation. Nicolas Cannasse |