Re: Which is faster ?
- Posted by mattlewis (admin) Feb 17, 2009
- 977 views
DerekParnell said...
Just a bit more clarification, find() is faster for long sequences. If the sequence is, say less than 10-15 items, then a loop can be faster.
Do you have any benchmarks that demonstrate this? I ran the following on r1380, and find() always won. I changed the length of the sequence, changed the values to be all integers (and used = instead of equal()), etc. Actually, the loop won one time, when s = {""}. There's just a lot more overhead in a euphoria loop than the native C loop in the backend find function (not to mention your optimizations for non-integer data).
sequence s = { 1,"2",3,4.4,5,6,7} constant REPS = 1000000 atom t = time() for i = 1 to REPS do for j = 1 to length(s) do object x = s[j] integer ix = find( j, s ) end for end for atom find_time = time() - t t = time() for i = 1 to REPS do for j = 1 to length(s) do for k = 1 to length(s) do object x = s[j] if equal( s[k], j ) then exit end if end for end for end for atom loop_time = time() - t printf(1, "%d repetitions:\nfind: %1.3f\nloop: %1.3f\n", { REPS, find_time, loop_time })
Matt