------ art_42430_6010091.1161828784727 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 10/25/06, Rick DeNatale <rick.denatale / gmail.com> wrote: > > On 10/25/06, Jeffrey Schwab <jeff / schwabcenter.com> wrote: > > Paul Lutus wrote: > > > Brad Tilley wrote: > > > > > >> Thanks for all the examples guys! Being Ruby, I thought there might > be > > >> some super simple method that just magically did this. I'll stick > with > > >> the old-fashioned modulo test. > > > > > > If speed matters, the modulo test is way slower than testing the LSB. > > > > Do you mean on a specific architecture, or is there a Ruby-specific > > reason for one to be slower than the other? > > Not really ruby-specific, and for the most part architecture independent. > > Modulo basically involves an integer division operation, whereas bit > testing is a simple logical and with a mask. > > (int & 1).zero? is probably faster than > int[0].zero? > > Since the latter generates the mask using the bit position. > > and both should be faster than > > (int % 2).zero? > > But it it's performance critical, benchmarking is recommended to > verify. One should never simply take 'rules of thumb' for granted. > > -- > Rick DeNatale > > My blog on Ruby > http://talklikeaduck.denhaven2.com/ > > An extremely quick "benchmark" 0.upto(1000000) do |i| (i & 1).zero? end 0.upto(1000000) do |i| i[0].zero? end 0.upto(1000000) do |i| (i % 2).zero? end I ran each of the above, and all of them averaged between, 500ms, and 550ms of sys time. So I'd say that realistically there is no significant performance difference in any of the methods. -- Tanner Burson tanner.burson / gmail.com http://tannerburson.com <---Might even work one day... ------ art_42430_6010091.1161828784727--