GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add benchmark for Array#new vs. Fixnum#times + map by Drenmi · Pull Request #91 · fastruby/fast-ruby

Conversation

@Drenmi

@Drenmi Drenmi commented Jan 11, 2016

Copy link
Copy Markdown
Contributor

When you need to map the result of a block invoked a fixed amount of times, you have an option between:

Array.new(n) { ... }

and:

n.times.map { ... }

The latter one is about 60% slower for n = 10, which goes down to around 40% for n > 1_000.

Note: logarithmic scale!

screen shot 2016-01-11 at 20 23 40

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

avellable and fatkodima reacted with thumbs up emoji
All reactions
  • 👍 2 reactions
@Drenmi
Add benchmark for Array#new vs. Fixnum#times + map
9be00e7
When you need to map the result of a block invoked a fixed amount
of times, you have an option between:

```
Array.new(n) { ... }
```

and:

```
n.times.map { ... }
```

The latter one is about 60% slower.
nateberkopec
nateberkopec reviewed Jan 11, 2016
View reviewed changes
Comment thread code/array/array-new-vs-fixnum-times-map.rb

@nateberkopec nateberkopec Jan 11, 2016

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this change with the number of elements?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Drenmi Drenmi Jan 11, 2016

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nateberkopec: It tapers off a bit, and stabilizes around 40% slower at 1_000 elements and up. Note: logarithmic scale!

screen shot 2016-01-11 at 20 23 40

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@nateberkopec nateberkopec Jan 11, 2016

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the graph! I'm just getting started with this repo, so someone else should comment, but whenever there's an Array or Enumerable benchmark I'm always curious with how it scales with # of elements. You may want to add a comment explaining that this behaves pretty much the same way at 1 million elements as it does at 10.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Drenmi Drenmi Jan 11, 2016

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problems @nateberkopec. :-) Is a comment in the PR sufficient, or do you want me to add it to the README?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@nateberkopec nateberkopec Jan 11, 2016

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shrug let's see what someone else thinks

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@deniskorobicyn deniskorobicyn Jan 11, 2016

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote for link to this PR in README.md.
This'll make info available for everyone, not only for people who reviewing PR's. Also, link will share that cool graph you have here:)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@JuanitoFatas JuanitoFatas Jan 11, 2016

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote for link to this PR in README.md.

👍

@Drenmi Awesome graph 👏 👏 👏

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Drenmi Drenmi Jan 12, 2016

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nateberkopec, @deniskorobicyn, @JuanitoFatas: Thanks for the kind words. 😃 I added the graph to the PR description, and linked to the PR from the README.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Drenmi
Add link to benchmark PR in README
bdfed07
As suggested by @deniskorobicyn, adding a link from the `README.md`
to the PR, which has more information on performance characteristics.
@Drenmi
Drenmi force-pushed the array-new-vs-fixnum-times-map branch from c778665 to bdfed07 Compare January 12, 2016 04:05
@Arcovion

Arcovion commented Jan 12, 2016

Copy link
Copy Markdown
Collaborator

There's a faster way:

require 'benchmark/ips'

LIMIT = 9

def fastest
  [].fill(0, LIMIT, &:itself)
end

def fast
  Array.new(LIMIT, &:itself)
end

def slow
  (0...LIMIT).map(&:itself)
end

def slowest
  LIMIT.times.map(&:itself)
end

Benchmark.ips do |bm|
  bm.report('Array#fill') { fastest }
  bm.report('Array#new') { fast }
  bm.report('Range#map') { slow }
  bm.report('Fixnum#times') { slowest }
  bm.compare!
end
Calculating -------------------------------------
          Array#fill    81.773k i/100ms
           Array#new    72.230k i/100ms
           Range#map    60.548k i/100ms
        Fixnum#times    56.625k i/100ms
-------------------------------------------------
          Array#fill      1.507M (± 1.4%) i/s -      7.605M
           Array#new      1.191M (± 1.7%) i/s -      5.995M
           Range#map    992.912k (± 1.9%) i/s -      4.965M
        Fixnum#times    868.483k (± 0.9%) i/s -      4.360M

Comparison:
          Array#fill:  1506707.9 i/s
           Array#new:  1191006.1 i/s - 1.27x slower
           Range#map:   992912.3 i/s - 1.52x slower
        Fixnum#times:   868483.2 i/s - 1.73x slower
rrosenblum reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@avellable

avellable commented Sep 23, 2016

Copy link
Copy Markdown
Collaborator

Let me add a reference to this.

@JuanitoFatas We can merge this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Drenmi

Drenmi commented Jan 8, 2017

Copy link
Copy Markdown
Contributor Author

Ping @JuanitoFatas. 😘

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ixti

ixti commented Jan 13, 2017

Copy link
Copy Markdown
Collaborator

@Drenmi can you please add what @Arcovion said?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kindoflew
kindoflew merged commit 1b4a509 into fastruby:master Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

8 participants

@Drenmi @Arcovion @avellable @ixti @nateberkopec @JuanitoFatas @deniskorobicyn @kindoflew
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.