Re: Better Way to Do This
- Posted by LarryMiller Aug 06, 2009
- 1123 views
DerekParnell said...
euphoric said...
Yeah, that's faster all right!
Sorry to burst the bubble, but the test is flawed.
Bernie's routines start from the back of the sequence and yours and mine from the front. Your test data had the atom as the last item so naturally, bernie's would find it first. Plus you forgot to reset the timer for the last test.
Here is some revised code.
include std/console.e atom t function no_atoms_1( sequence s ) integer i = 1 for t=1 to length(s) do if atom(s[t]) then return 0 end if end for return 1 end function function no_atoms_bernie_1( sequence s ) integer i = length(s) while sequence(s[i]) and i > 0 do i -= 1 end while if i=0 then return 1 else return 0 end if end function function no_atoms_bernie_2( sequence s ) integer i = length(s) while sequence(s[i]) and i > 0 do i -= 1 end while return not i end function function no_atoms_stdlib( sequence x ) for i = 1 to length(x) do if not sequence(x[i]) then return 0 end if end for return 1 end function sequence a -- Put atom in the middle. a = repeat({},250) & 1 & repeat({},250) t = time() for x=1 to 100000 do no_atoms_1( a ) end for ?time()-t t = time() for x=1 to 100000 do no_atoms_bernie_1( a ) end for ?time()-t t = time() for x=1 to 100000 do no_atoms_bernie_2( a ) end for ?time()-t t = time() for x=1 to 100000 do no_atoms_stdlib( a ) end for ?time()-t wait_key()
I get these results ...
c:\temp>eui test 0.5 0.641 0.656 0.469
euphoric said...
I do have a question. When i gets to zero, why doesn't the test for sequence(s[i]) fail? Or is 0 a valid sequence element?!
Good question ... don't know the answer yet.
The answer is simple. i never reaches 0, the loop exits when i is 1. I discovered this when I changed the data to include only sequences. i then reached 0 and produced the usual error.