Skip to content

Optimisation#23

Open
VladFiliucov wants to merge 19 commits intospajic:masterfrom
VladFiliucov:optimisation
Open

Optimisation#23
VladFiliucov wants to merge 19 commits intospajic:masterfrom
VladFiliucov:optimisation

Conversation

@VladFiliucov
Copy link

Лучше поздно, чем никогда!

Copy link
Owner

@spajic spajic left a comment

Choose a reason for hiding this comment

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

👍 Очень хорошая работа, приятно почитать!


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

Choose a reason for hiding this comment

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

🎩


## 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_

Copy link
Owner

Choose a reason for hiding this comment

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

Хорошо про life is to short 👍

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).
Copy link
Owner

Choose a reason for hiding this comment

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

По идее # 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.
Copy link
Owner

Choose a reason for hiding this comment

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

👍 плюсик за книжку

xit 'performs linear' do
expect { |n, i|
work("sample_data/#{n}_lines.txt")
}.to perform_linear.in_range(100, 300).ratio(100)
Copy link
Owner

Choose a reason for hiding this comment

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

Хм, есть ещё Minitest::Benchmark#assert_performance_linear - вроде бы работает консистентно.

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.

2 participants