Re: Better Way to Do This
- Posted by euphoric (admin) Aug 06, 2009
- 1124 views
bernie said...
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?!