Joel VanderWerf
11/2/2007 2:05:00 AM
theosib@gmail.com wrote:
> ...Anyhow, so I'm trying to find the fastest way to
> compute a dot product...
require 'narray' # not pure ruby
NVector[1,2,3] * NVector[3,6,9]
=> 42
Here's a benchmark:
require 'benchmark'
require 'narray'
def dot_product_e l1, l2
sum = 0
for i in 0...l1.size
sum += l1[i] * l2[i]
end
sum
end
Benchmark.bmbm(12) do |x|
n = 100_000
u = NVector[1..20]
v = NVector[21..40]
x.report("u*v") do
n.times do
u*v
end
end
x.report("dot_product_e") do
n.times do
dot_product_e u, v
end
end
end
__END__
Output:
Rehearsal -------------------------------------------------
u*v 0.380000 0.020000 0.400000 ( 0.396224)
dot_product_e 3.370000 0.000000 3.370000 ( 3.382755)
---------------------------------------- total: 3.770000sec
user system total real
u*v 0.390000 0.000000 0.390000 ( 0.398509)
dot_product_e 3.380000 0.010000 3.390000 ( 3.385840)
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407