Created
April 25, 2016 19:20
-
-
Save lokeshh/73ca4d3efa30a0e41a126f881bc28f9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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