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

prune: speed up using 1<<N instead of math.Pow(2, N) #76

Merged

Conversation

odeke-em
Copy link
Contributor

This change fixes the use of expensive code that used math.Pow(float(2), N) but really that's expensive code that could be simplified by using left bitshifts.

The improvements in speed show in benchmarks:

$ benchstat before.txt after.txt
name    old time/op    new time/op    delta
Pow2-8     322ns ± 2%     114ns ± 1%  -64.69%  (p=0.000 n=9+8)

name    old alloc/op   new alloc/op   delta
Pow2-8     40.0B ± 0%     40.0B ± 0%     ~     (all equal)

name    old allocs/op  new allocs/op  delta
Pow2-8      5.00 ± 0%      5.00 ± 0%     ~     (all equal)

Fixes #72

@odeke-em odeke-em force-pushed the convert-math.Pow2-to-left-bitshift branch from 701097c to 4b9976f Compare October 17, 2022 19:47
Copy link
Member

@liamsi liamsi left a comment

Choose a reason for hiding this comment

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

Thanks!

This change fixes the use of expensive code that used
math.Pow(float(2), N) but really that's expensive code
that could be simplified by using left bitshifts.

The improvements show up in benchmarks:

```shell
$ benchstat before.txt after.txt
name    old time/op    new time/op    delta
Pow2-8     322ns ± 2%     114ns ± 1%  -64.69%  (p=0.000 n=9+8)

name    old alloc/op   new alloc/op   delta
Pow2-8     40.0B ± 0%     40.0B ± 0%     ~     (all equal)

name    old allocs/op  new allocs/op  delta
Pow2-8      5.00 ± 0%      5.00 ± 0%     ~     (all equal)
```

Fixes celestiaorg#72
@odeke-em odeke-em force-pushed the convert-math.Pow2-to-left-bitshift branch from 4b9976f to a49a6c0 Compare October 21, 2022 00:36
@liamsi liamsi merged commit 3b1802d into celestiaorg:master Oct 25, 2022
@odeke-em odeke-em deleted the convert-math.Pow2-to-left-bitshift branch October 25, 2022 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants