Skip to content

Instantly share code, notes, and snippets.

@fbernier
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save fbernier/9231807 to your computer and use it in GitHub Desktop.

Select an option

Save fbernier/9231807 to your computer and use it in GitHub Desktop.

Revisions

  1. fbernier revised this gist Feb 26, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions lazy.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    # Enumerator#lazy is very slow and should only be used when iterating over large/infinite collections
    # where you know you are going to get your results quite early in the iteration.

    # This benchmark is purposely made to advantage the lazy version, without much success.

    require 'benchmark/ips'

    class Enumerator::Lazy
  2. fbernier revised this gist Feb 26, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions lazy.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Enumerator#lazy is quite slow and should only be used when iterating over large collections
    # where you need few results and you know you are going to get them quite early in the iteration.
    # Enumerator#lazy is very slow and should only be used when iterating over large/infinite collections
    # where you know you are going to get your results quite early in the iteration.

    require 'benchmark/ips'

  3. fbernier revised this gist Feb 26, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion lazy.rb
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    # Enumerator#lazy is quite slow and should only be used when iterating over large collections
    where you need few results and you know you are going to get them quite early in the iteration.
    # where you need few results and you know you are going to get them quite early in the iteration.

    require 'benchmark/ips'

  4. fbernier revised this gist Feb 26, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion lazy.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Enumerator#lazy is quite slow and should only be used when iterating over large collections where you need few results and you know you are going to get them quite early in the iteration.
    # Enumerator#lazy is quite slow and should only be used when iterating over large collections
    where you need few results and you know you are going to get them quite early in the iteration.

    require 'benchmark/ips'

  5. fbernier revised this gist Feb 26, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions lazy.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Enumerator#lazy is quite slow and should only be used when iterating over large collections where you need few results and you know you are going to get them quite early in the iteration.

    require 'benchmark/ips'

    class Enumerator::Lazy
  6. fbernier revised this gist Feb 26, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion lazy.rb
    Original file line number Diff line number Diff line change
    @@ -22,4 +22,6 @@ def filter_map
    Calculating -------------------------------------
    map+compact 12035 i/100ms
    filter_map 10588 i/100ms
    -------------------------------------------------
    -------------------------------------------------
    map+compact 135411.8 (±0.9%) i/s - 685995 in 5.066383s
    filter_map 114386.4 (±4.9%) i/s - 571752 in 5.010041s
  7. fbernier created this gist Feb 26, 2014.
    25 changes: 25 additions & 0 deletions lazy.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    require 'benchmark/ips'

    class Enumerator::Lazy
    def filter_map
    Lazy.new(self) do |yielder, *values|
    result = yield *values
    yielder << result if result
    end
    end
    end

    Benchmark.ips do |r|
    r.report("map+compact") do
    (0..50).map{|a| 'lol' if a.even?}.compact.first(5)
    end

    r.report("filter_map") do
    (0..50).lazy.filter_map{|a| 'lol' if a.even?}.first(5)
    end
    end

    Calculating -------------------------------------
    map+compact 12035 i/100ms
    filter_map 10588 i/100ms
    -------------------------------------------------