Open
Conversation
spajic
approved these changes
Apr 17, 2019
Owner
spajic
left a comment
There was a problem hiding this comment.
👍 Очень хорошая работа, приятно почитать!
|
|
||
| We already had a ruby program that was doing the job, but unfortunately it didn't scale well. At some point it started working so slow that we where not sure wether it's going to finish processing in some reasonable time. | ||
|
|
||
| So I grabbed my optimisation hat. |
|
|
||
| ## Establishing metrics | ||
| To measure the impact changes are having on our program I needed some reliable metrics. So first I created few sample files for 10, 100, 1k, 10k, 20k, and 30k lines each.First I measured iterations per second usign (with [benchmark-ips](https://github.com/evanphx/benchmark-ips) gem) each file as argument for main method. I turned out that 30k performed 76062 times slower then 10 lines in context of ips. But benchmark-ips can't do wall time so had to reach out for benchmark from ruby standard library. It apreas that it takes 4 seconds to process 10k, and 59,5 seconds to process. Life is too short to wait a minute each time so I stoped on 20k lines which would perform in 26 seconds and where 36876 times slower then 10 lines in terms of ips. I can work with that. So now we have our _base case: 20k lines performing in 26 seconds_ | ||
|
|
| sessions = sessions + [parse_session(line)] if cols[0] == 'session' | ||
| end | ||
| ``` | ||
| First thing I've extracted string literals into constant and froze them. Also added magic frozen string literal comment at start of the file. (Only this reduced allocation by 80k objects and speeded up base metrics to 22,7 seconds). Then avoided creating extra arrays and edited original arrays in place (what reduced allocation on 4k more). |
Owner
There was a problem hiding this comment.
По идее # frozen_string_literal: true должно быть достаточно, можно было бы отдельные константы не фризить.
| 5.26 MB task-1.rb:103 | ||
| ``` | ||
|
|
||
| Then I used a trick I found in Ruby Performance Optimisation which I was reading in breaks between the optimisations and got rid of date parsings completely. |
| xit 'performs linear' do | ||
| expect { |n, i| | ||
| work("sample_data/#{n}_lines.txt") | ||
| }.to perform_linear.in_range(100, 300).ratio(100) |
Owner
There was a problem hiding this comment.
Хм, есть ещё Minitest::Benchmark#assert_performance_linear - вроде бы работает консистентно.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Лучше поздно, чем никогда!