Hi,
Did you do any benchmarks? I did a quick, creating 10000 arrays
of random size between 0-1000 (a couple preliminary tests of size
0-10000 elements gave a similar trend). On average, I get
results like:
byte code:
jones 6.9 (from CVS)
hurt 7.2 (from email)
skaller 5.8 (list with remembered size - see below)
native code:
jones 0.82
hurt 0.80
skaller 0.72
So, it would seem that the version which uses lists + length as an
intermediate representation is the fastest. Caveat: I probably
didn't run it long enough/with large enough arrays to test the effects
of garbage collection. Brian's version might be better in these cases,
but that may require more in-depth testing. But Max's version is
much simpler! :)
I think these results suggest that it may make a lot of sense to have
find_all return a list. (As Max suggested, perhaps a common
function called by both find_all and filter, which returns list *
length).
-Amit
(Code for skaller below)
let filter f a =
let result = ref [] in
let n = ref 0 in
Array.iter
(fun x ->
if f x then begin
result := x :: !result;
incr n
end)
a;
match !result with
| [] -> [| |]
| x::xs ->
let filtered = Array.create !n x in
let rec fill i = function
| [] -> filtered
| x::xs -> filtered.(i) <- x; fill (i+1) xs
in
fill 1 xs;;
|
Try Searching:
servers, voip, java, networking, microsoft ...
|
|
|
|