https://github.com/konstantin8105/pow
replace golang math.Pow to optimal
https://github.com/konstantin8105/pow
bench golang pow power-method
Last synced: about 1 year ago
JSON representation
replace golang math.Pow to optimal
- Host: GitHub
- URL: https://github.com/konstantin8105/pow
- Owner: Konstantin8105
- License: mit
- Created: 2018-12-30T18:20:15.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-13T16:28:49.000Z (about 3 years ago)
- Last Synced: 2024-11-17T12:16:50.011Z (over 1 year ago)
- Topics: bench, golang, pow, power-method
- Language: Go
- Size: 5.86 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pow
replace golang math.Pow to optimal
#### godoc
```
FUNCTIONS
func E2(x float64) float64
E2 function replace `math.Pow(x,2.0)`
func E3(x float64) float64
E3 function replace `math.Pow(x,3.0)`
func E4(x float64) float64
E4 function replace `math.Pow(x,4.0)`
func En(x float64, e int) float64
En function replace `math.Pow(x,e)`
```
#### benchmark
```
go test -bench=. -count=5 > bench.txt
benchstat bench.txt
```
```
name time/op
/math.Pow2-4 41.7ns ±11%
/pow.E2-4 0.60ns ± 9%
/pow.En2-4 6.88ns ± 1%
/math.Pow3-4 39.1ns ± 1%
/pow.E3-4 0.59ns ± 2%
/pow.En3-4 7.80ns ± 2%
/math.Pow4-4 42.8ns ± 2%
/pow.E4-4 0.58ns ± 0%
/pow.En4-4 7.76ns ± 3%
/math.Pow(x,_51)-4 46.3ns ± 0%
/pow.En(x,_51)-4 16.8ns ± 0%
```
So, that package is more optimal at more 30 times.
#### example
```golang
func Example() {
x := 2.0
r2 := pow.E2(x) // math.Pow(x, 2.0)
r3 := pow.E3(x) // math.Pow(x, 3.0)
fmt.Fprintf(os.Stdout, "%.4f\n", r2)
fmt.Fprintf(os.Stdout, "%.4f\n", r3)
// Output:
// 4.0000
// 8.0000
}
```