Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark for Array#new vs. Fixnum#times + map #91

Merged
merged 2 commits into from
Apr 25, 2022

Conversation

Drenmi
Copy link
Contributor

@Drenmi Drenmi commented Jan 11, 2016

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

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.
@@ -0,0 +1,17 @@
require "benchmark/ips"

ELEMENTS = 9
Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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

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:)

Copy link
Contributor

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 👏 👏 👏

Copy link
Contributor Author

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.

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
Copy link
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

@avellable
Copy link
Collaborator

Let me add a reference to this.

@JuanitoFatas We can merge this.

@Drenmi
Copy link
Contributor Author

Drenmi commented Jan 8, 2017

Ping @JuanitoFatas. 😘

@ixti
Copy link
Collaborator

ixti commented Jan 13, 2017

@Drenmi can you please add what @Arcovion said?

@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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants