IP Address to Decimal - is an one-liner possible?

S

s.ross

# of iterations = 1000000
user system total real
null_time 0.218000 0.000000 0.218000 ( 0.219000)
split/each/<<+ 24.516000 0.140000 24.656000 ( 24.703000)
split/map/<<+ 26.109000 0.172000 26.281000 ( 26.281000)
split/inject/<<+ 30.797000 0.172000 30.969000 ( 31.000000)

Being an FP nut used to using folds, I'm always a little annoyed that
inject fares so poorly in these things; mainly we're just lacking a
C implementation of Array#inject.

coming back to this point... what do you mean by we're missing a C
impl of Array#inject? Granted, the impl is on Enumerable, but I
wouldn't think it would make THAT much of a difference... But I try
my best not to speculate (ever), so...

(my C coding skills absolutely suck these days, please point out
improvements)

#!/usr/bin/env ruby -w

require 'benchmark'
require 'ipaddr'
$: << File.expand_path("~/Work/p4/zss/src/RubyInline/dev/lib")
require 'inline'

class Array
inline do |builder|
builder.c_raw <<-EOF
VALUE new_inject(int argc, VALUE *argv, VALUE self) {
long max = RARRAY(self)->len;
long i = argc ? 0 : 1;
VALUE memo = argc ? argv[0] : RARRAY(self)->ptr[0];

for (i; i < max; i++) {
memo = rb_yield_values(2, memo, RARRAY(self)->ptr);
}

return memo;
}
EOF
end
end

p "127.0.0.1".split('.').inject(0) { |s,n| (s << 8) + n.to_i }
p "127.0.0.1".split('.').new_inject(0) { |s,n| (s << 8) + n.to_i }

max = (ARGV.shift || 1_000_000).to_i

# # of iterations = 1000000
# user system total real
# null_time 0.130000 0.000000 0.130000 ( 0.129965)
# split/each/<< 10.940000 0.010000 10.950000 ( 10.968329)
# split/inject/<< 15.280000 0.020000 15.300000 ( 15.330062)
# split/new_inject/<< 15.020000 0.070000 15.090000 ( 15.629343)


The question I would ask is not why does inject suck, but why doesn't
each suck? There is only so much you can optimize in C, and you've
really only optimized the loop -- not the arithmetic operation because
that is yielded back to Ruby.
 
B

brabuhr

coming back to this point... what do you mean by we're missing a C
impl of Array#inject? Granted, the impl is on Enumerable, but I
wouldn't think it would make THAT much of a difference... But I try my
best not to speculate (ever), so...

(my C coding skills absolutely suck these days, please point out
improvements)
...
# # of iterations = 1000000
# user system total real
# null_time 0.130000 0.000000 0.130000 ( 0.129965)
# split/each/<< 10.940000 0.010000 10.950000 ( 10.968329)
# split/inject/<< 15.280000 0.020000 15.300000 ( 15.330062)
# split/new_inject/<< 15.020000 0.070000 15.090000 ( 15.629343)
Linux linux116.ctc.com 2.6.18-53.1.13.el5 #1 SMP Tue Feb 12 13:01:45
EST 2008 i686 i686 i386 GNU/Linux
ruby 1.8.5 (2006-08-25) [i386-linux]
gcc version 4.1.2 20070626 (Red Hat 4.1.2-14)

# of iterations = 1000
user system total real
null_time 0.010000 0.000000 0.010000 ( 0.000750)
split/each/<< 0.010000 0.000000 0.010000 ( 0.015283)
split/inject/<< 0.020000 0.000000 0.020000 ( 0.027374)
split/new_inject/<< 0.020000 0.000000 0.020000 ( 0.016135)

# of iterations = 10000
user system total real
null_time 0.010000 0.000000 0.010000 ( 0.007443)
split/each/<< 0.120000 0.040000 0.160000 ( 0.166371)
split/inject/<< 0.180000 0.060000 0.240000 ( 0.232431)
split/new_inject/<< 0.140000 0.030000 0.170000 ( 0.173137)

# of iterations = 100000
user system total real
null_time 0.040000 0.030000 0.070000 ( 0.075336)
split/each/<< 1.260000 0.370000 1.630000 ( 1.641662)
split/inject/<< 1.670000 0.500000 2.170000 ( 2.184967)
split/new_inject/<< 1.390000 0.350000 1.740000 ( 1.755857)

# of iterations = 1000000
user system total real
null_time 0.390000 0.320000 0.710000 ( 0.770740)
split/each/<< 12.490000 3.530000 16.020000 ( 16.168579)
split/inject/<< 16.720000 5.100000 21.820000 ( 22.276735)
split/new_inject/<< 13.800000 3.500000 17.300000 ( 17.358260)

# of iterations = 10000000
user system total real
null_time 4.350000 3.030000 7.380000 ( 7.429744)
split/each/<< 125.350000 35.750000 161.100000 (162.263233)
split/inject/<< 166.610000 51.400000 218.010000 (219.308560)
split/new_inject/<< 135.410000 36.080000 171.490000 (171.935555)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,212
Latest member
BrennaCaba

Latest Threads

Top