]> Cypherpunks repositories - gostls13.git/commitdiff
math: optimize sinh and cosh
authorerifan01 <eric.fang@arm.com>
Tue, 26 Dec 2017 01:49:37 +0000 (01:49 +0000)
committerRobert Griesemer <gri@golang.org>
Tue, 27 Feb 2018 04:34:37 +0000 (04:34 +0000)
Improve performance by reducing unnecessary function calls

Benchmarks:

Tme    old time/op  new time/op  delta
Cosh-8   229ns ± 0%   138ns ± 0%  -39.74%  (p=0.008 n=5+5)
Sinh-8   231ns ± 0%   139ns ± 0%  -39.83%  (p=0.008 n=5+5)

Change-Id: Icab5485849bbfaafca8429d06b67c558101f4f3c
Reviewed-on: https://go-review.googlesource.com/85477
Reviewed-by: Robert Griesemer <gri@golang.org>
src/math/sinh.go

index 30bbc0661ec949a5dbf7d97ae7e48457bc5ada5d..39e7c2047a9ec4b2e08da06a64619280bef01c26 100644 (file)
@@ -45,10 +45,11 @@ func sinh(x float64) float64 {
        var temp float64
        switch true {
        case x > 21:
-               temp = Exp(x) / 2
+               temp = Exp(x) * 0.5
 
        case x > 0.5:
-               temp = (Exp(x) - Exp(-x)) / 2
+               ex := Exp(x)
+               temp = (ex - 1/ex) * 0.5
 
        default:
                sq := x * x
@@ -73,7 +74,8 @@ func Cosh(x float64) float64
 func cosh(x float64) float64 {
        x = Abs(x)
        if x > 21 {
-               return Exp(x) / 2
+               return Exp(x) * 0.5
        }
-       return (Exp(x) + Exp(-x)) / 2
+       ex := Exp(x)
+       return (ex + 1/ex) * 0.5
 }