Re: Better Way to Do This
- Posted by Fernando Aug 06, 2009
- 1103 views
Would this be faster ?
function no_atoms( 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
Yeah, that's faster all right! Here's the program I used to test the speeds. Bernie, your suggested code was waaaaaay faster than any other. It should definitely replace our std/lib sequence_array()!
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( object x ) if not sequence(x) then return 0 end if 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 a = repeat({},500) a &= 1 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 for x=1 to 100000 do no_atoms_stdlib( a ) end for ?time()-t wait_key()
The no_atoms_bernie_2() was just my testing of the "not" thingie. Didn't help.
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?!
Probably, it's faster because in your example you put the atom at the end of the sequence, and Bernie's implementation begins from this point.
Depending on the uniformity of your sequences, it's possible to improve the speed if you store (cache) the index of the sequence when the function aborts. Then, on the next call, the circular search would begin at that stored point.
Regards,
Fernando