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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,25 @@ Comparison:
Array#insert: 0.2 i/s - 262.56x slower
```

##### `Array#new` vs `Fixnum#times + map` [code](code/array/array-new-vs-fixnum-times-map.rb)

Typical slowdown is 40-60% depending on the size of the array. See the corresponding
[pull request](https://github.com/JuanitoFatas/fast-ruby/pull/91/) for performance characteristics.

```
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]
Calculating -------------------------------------
Array#new 63.875k i/100ms
Fixnum#times + map 48.010k i/100ms
-------------------------------------------------
Array#new 1.070M (± 2.2%) i/s - 5.365M
Fixnum#times + map 678.097k (± 2.7%) i/s - 3.409M

Comparison:
Array#new: 1069837.0 i/s
Fixnum#times + map: 678097.4 i/s - 1.58x slower
```

### Enumerable

##### `Enumerable#each + push` vs `Enumerable#map` [code](code/enumerable/each-push-vs-map.rb)
Expand Down
17 changes: 17 additions & 0 deletions code/array/array-new-vs-fixnum-times-map.rb
Original file line number Diff line number Diff line change
@@ -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.


def fast
Array.new(ELEMENTS) { |i| i }
end

def slow
ELEMENTS.times.map { |i| i }
end

Benchmark.ips do |x|
x.report("Array#new") { fast }
x.report("Fixnum#times + map") { slow }
x.compare!
end