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

Improve speed of shannon and ginisimpson #135

Merged
merged 1 commit into from
Aug 19, 2022
Merged

Improve speed of shannon and ginisimpson #135

merged 1 commit into from
Aug 19, 2022

Conversation

barucden
Copy link
Contributor

@barucden barucden commented Aug 8, 2022

Hi. I noticed that some computations could be saved in the definition of shannon and ginisimpson.

I am not a user of this package, so I am not sure about the impact. However, some measurements:

julia> const x = rand(1000);

julia> @btime shannon(x)
  148.720 μs (2 allocations: 15.88 KiB)
6.730219402261953

julia> @btime new_shannon(x)
  11.158 μs (0 allocations: 0 bytes)
6.730219402261952

julia> @btime ginisimpson(x)
  4.278 μs (2 allocations: 15.88 KiB)
0.9986907680545117

julia> @btime new_ginisimpson(x)
  296.874 ns (0 allocations: 0 bytes)
0.9986907680545117

The speed difference should increase with the length of the input.

Be aware that the result of the new shannon is little bit off due to numerical accuracy.

This commit improves the speed of `shannon` and `ginisimpsons` by
avoiding creation of temporary arrays. The `shannon` function is also
simplified a little.
@kescobo kescobo merged commit ada4530 into EcoJulia:main Aug 19, 2022
@kescobo
Copy link
Member

kescobo commented Aug 19, 2022

🤔 Ack! Sorry for this languishing. I could have sworn I replied to this and merged it already... 😱

Anyway, looks great - these functions are rarely bottlenecks, but more speed with no cost is obviously great. Thanks for the contribution!

@barucden barucden deleted the speed-up-diversity branch August 25, 2022 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants