In article <4002BDA5.7080906 / wanadoo.fr>, Emmanuel Touzery <emmanuel.touzery / wanadoo.fr> wrote: >David Garamond wrote: > >> 1. Is there a way in Ruby to speed up 32bit integer arithmetics (only >> involving numbers & sums up to 2^32-1)? I want to use Ruby for >> summarizing network traffic logs, but it's pathetically slow compared >> to Perl: >> >> $ time ruby -e'1000000.times{1073741823+1073741824}' >> >> real 0m23.693s >> user 0m5.720s >> sys 0m0.610s >> $ time perl -e'for(1..1000000){1073741823+1073741824}' >> >> real 0m1.142s >> user 0m0.320s >> sys 0m0.050s >> >> since 2**30 is already in the Bignum range. > >i have the feeling (unconfirmed) that perl is optimising it away since >the result is unused or the operation repeated or something. i created a >file with 1000000 lines repeating the addition and perl is very slow too >then (and ruby too). > >i'm not sure. You are correct about perl optimising away a constant - [mike@ratdog mike]$ perl -MO=Deparse -e 'for(1..1000000){1073741823+1073741824}' foreach $_ (1 .. 1000000) { '???'; } -e syntax OK ??? is B::Deparse's way of showing something that's been optimised away. If you make perl do some work then it does slow down e.g. [mike@ratdog mike]$ time perl -e 'for(1..1000000){1073741823+1073741824}' 0.12user 0.00system 0:00.26elapsed 45%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (297major+36minor)pagefaults 0swaps [mike@ratdog mike]$ time perl -e 'for(1..1000000){$_+1073741824}' 0.24user 0.00system 0:00.24elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (300major+36minor)pagefaults 0swaps [mike@ratdog mike]$ perl -MO=Deparse -e 'for(1..1000000){$_+1073741824}' foreach $_ (1 .. 1000000) { $_ + 1073741824; } -e syntax OK Hope this helps, Mike -- mike / stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike / exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA