Skip to content

Instantly share code, notes, and snippets.

@lokeshh
Created April 25, 2016 19:20
Show Gist options
  • Select an option

  • Save lokeshh/73ca4d3efa30a0e41a126f881bc28f9f to your computer and use it in GitHub Desktop.

Select an option

Save lokeshh/73ca4d3efa30a0e41a126f881bc28f9f to your computer and use it in GitHub Desktop.
# Code for benchmark
require 'benchmark'
vector = Daru::Vector.new(
10000.times.map.to_a.shuffle,
missing_values: 100.times.map.to_a.shuffle
)
Benchmark.bm do |x|
x.report("Sum of a vector using compact") do
vector.data.sum_using_compact
end
x.report("Sum of a vector without compact") do
vector.data.sum_without_compact
end
end
# END
# code for sum
def compact
@data - @context.missing_values
end
def sum_using_compact
compact.inject :+
end
def sum_without_compact
@data.inject do |sum, x|
unless @context.missing_values.include? x
sum += x
end
sum
end
end
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment