-
Notifications
You must be signed in to change notification settings - Fork 5
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
Optimise English implementation #53
Conversation
I'm worried that these optimisations aren't actually optimising anything! I have had success in the past with using julia> @btime spelled_out($123456789, lang = $:en_UK) # master at dc74774
8.797 μs (204 allocations: 4.39 KiB)
"one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine"
julia> @btime spelled_out($123456789, lang = $:en_UK) # optimisation at 22e2773
9.906 μs (220 allocations: 5.83 KiB)
"one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine" |
Instead of creating IOBuffers within each function, pass around IOBuffers to modifying functions and only take from them at the end of the process! This vastly reduces the allocations as we are no longer creating a new IOBuffer within each function. However, it is still not as fast as it was (but its memory usage is better).
Since last commit, I figured that creating The benchmarking results are as follows. Indeed, it uses fewer allocations. However, I wonder where it is spending the most time? It is not as fast as the others (in fact, it might even be slower than all of the other versions). julia> @btime spelled_out(123456789, lang = :en_GB) # optimisation at 3175b96
10.275 μs (190 allocations: 3.92 KiB)
"one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine" |
For some reason, on a whim I changed julia> @btime spelled_out(123456789, lang = :en_GB) # optimisation at a9403ed
10.119 μs (190 allocations: 3.92 KiB)
"one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine"
julia> @btime spelled_out(123456789, lang = :en_GB) # optimisation at 00f458a
9.296 μs (190 allocations: 3.92 KiB)
"one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine" |
I don't know why, but I was being awfully liberal with using BigInts in this code. Reducing when I use these has greatly optimised this julia> @btime spelled_out(123456789, lang = :en_GB) # optimisation at 7fb5af2
6.499 μs (120 allocations: 2.70 KiB)
"one hundred and twenty-three million, four hundred and fifty-six thousand, seven hundred and eighty-nine" |
Instead of using a weird `reverse` method for string manipulation in printing of ordinals, we use a util function. Also, use `IOBuffer` where practical for alt convert methods
I think this is probably as optimised as I'm going to get it. The attached |
Optimise English implementation
Coming back to this, there are changes I'd like to make regarding optimisation. I started these a while ago, but never pushed anything. Figured I should open a PR to track these changes.