go/src/math
Riccardo Gerosa 91a1f0d918 math/big: improve performance of Binomial
This change improves the performance of Binomial by implementing an
algorithm that produces smaller intermediate values at each step.

Working with smaller big.Int values has the advantage that  fewer allocations
and computations are required for each mathematical operation.

The algorithm used is the Multiplicative Formula, which is a well known
way of calculating the Binomial coefficient and is described at:
https://en.wikipedia.org/wiki/Binomial_coefficient#Multiplicative_formula
https://en.wikipedia.org/wiki/Binomial_coefficient#In_programming_languages

In addition to that, an optimization has been made to remove a
redundant computation of (i+1) on each loop which has a measurable
impact when using big.Int.

Performance improvement measured on an M1 MacBook Pro
running the existing benchmark for Binomial:

name        old time/op    new time/op    delta
Binomial-8     589ns ± 0%     435ns ± 0%  -26.05%  (p=0.000 n=10+10)

name        old alloc/op   new alloc/op   delta
Binomial-8    1.02kB ± 0%    0.08kB ± 0%  -92.19%  (p=0.000 n=10+10)

name        old allocs/op  new allocs/op  delta
Binomial-8      38.0 ± 0%       5.0 ± 0%  -86.84%  (p=0.000 n=10+10)

Change-Id: I5a830386dd42f062e17af88411dd74fcb110ded9
GitHub-Last-Rev: 6b2fca07de
GitHub-Pull-Request: golang/go#56339
Reviewed-on: https://go-review.googlesource.com/c/go/+/444315
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
2022-10-28 23:50:33 +00:00
..
big math/big: improve performance of Binomial 2022-10-28 23:50:33 +00:00
bits math/bits: note that functions here may be compiler intrinsics 2022-10-21 17:51:06 +00:00
cmplx all: gofmt main repo 2022-04-11 16:34:30 +00:00
rand math/rand: deprecate Read 2022-10-27 04:32:55 +00:00
abs.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
acos_s390x.s
acosh.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
acosh_s390x.s
all_test.go math: don't use integer division that truncates to zero 2022-03-05 21:19:15 +00:00
arith_s390x.go
arith_s390x_test.go
asin.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
asin_s390x.s
asinh.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
asinh_s390x.s
atan.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
atan2.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
atan2_s390x.s
atan_s390x.s
atanh.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
atanh_s390x.s
bits.go all: replace `` and '' with “ (U+201C) and ” (U+201D) in doc comments 2022-04-05 17:52:29 +00:00
cbrt.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
cbrt_s390x.s
const.go math: show value of integer constants in comments 2022-09-21 14:07:39 +00:00
const_test.go math: add MaxUint, MinInt, MaxInt 2021-05-03 22:44:33 +00:00
copysign.go math: improve documentation of Copysign 2022-04-14 17:42:53 +00:00
cosh_s390x.s
dim.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
dim_amd64.s
dim_arm64.s
dim_asm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
dim_noasm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
dim_riscv64.s
dim_s390x.s
erf.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
erf_s390x.s
erfc_s390x.s
erfinv.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
example_test.go math: add Remainder example 2021-10-07 18:09:53 +00:00
exp.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
exp2_asm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exp2_noasm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exp_amd64.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exp_amd64.s
exp_arm64.s
exp_asm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exp_noasm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
exp_s390x.s
expm1.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
expm1_s390x.s
export_s390x_test.go
export_test.go
floor.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
floor_386.s
floor_amd64.s
floor_arm64.s
floor_asm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
floor_noasm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
floor_ppc64x.s all: add //go:build lines to assembly files 2021-05-13 09:12:17 +00:00
floor_s390x.s
floor_wasm.s
fma.go
frexp.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
gamma.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
huge_test.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
hypot.go math: optimize Hypot function 2022-10-01 01:51:35 +00:00
hypot_386.s
hypot_amd64.s
hypot_asm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
hypot_noasm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
j0.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
j1.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
jn.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
ldexp.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
lgamma.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
log.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
log1p.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
log1p_s390x.s
log10.go
log10_s390x.s
log_amd64.s
log_asm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
log_s390x.s
log_stub.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
logb.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
mod.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
modf.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
modf_arm64.s
modf_asm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
modf_noasm.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
modf_ppc64x.s all: add //go:build lines to assembly files 2021-05-13 09:12:17 +00:00
nextafter.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
pow.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
pow10.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
pow_s390x.s
remainder.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
signbit.go
sin.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
sin_s390x.s
sincos.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
sinh.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
sinh_s390x.s all: fix typos 2021-08-17 13:54:10 +00:00
sqrt.go cmd/compile,math: remove all sqrt assembly code 2022-08-15 17:07:57 +00:00
stubs.go all: go fix -fix=buildtag std cmd (except for bootstrap deps, vendor) 2021-10-28 18:17:57 +00:00
stubs_s390x.s
tan.go all: fix some lint issues 2022-05-08 17:27:54 +00:00
tan_s390x.s all: fix typos 2021-08-17 13:54:10 +00:00
tanh.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
tanh_s390x.s
trig_reduce.go all: gofmt main repo 2022-04-11 16:34:30 +00:00
unsafe.go