feat: working benchmark of pocomath
This commit is contained in:
parent
0dbb95bbbe
commit
f1a2d04f4b
3 changed files with 76 additions and 0 deletions
43
benchmark/roots.mjs
Normal file
43
benchmark/roots.mjs
Normal file
|
@ -0,0 +1,43 @@
|
|||
import Benchmark from 'benchmark'
|
||||
import padRight from 'pad-right'
|
||||
import math from '../src/pocomath.mjs'
|
||||
|
||||
function pad (text) {
|
||||
return padRight(text, 40, ' ')
|
||||
}
|
||||
|
||||
const maxCoeff = 5
|
||||
function countRoots () {
|
||||
let polys = 0
|
||||
let roots = 0
|
||||
for (let d = 0; d <= maxCoeff; ++d) {
|
||||
for (let c = 0; c <= maxCoeff; ++c) {
|
||||
for (let b = 0; b <= maxCoeff; ++b) {
|
||||
for (let a = 1; a <= maxCoeff; ++a) {
|
||||
polys += 1
|
||||
roots += math.length(math.polynomialRoot(d, c, b, a))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return [polys, roots]
|
||||
}
|
||||
|
||||
const test = countRoots()
|
||||
console.log('There are', test[1], 'roots of the', test[0], 'integer cubic')
|
||||
console.log('polynomials (with coefficients <=', maxCoeff, ')')
|
||||
|
||||
const results = []
|
||||
|
||||
const suite = new Benchmark.Suite()
|
||||
suite
|
||||
.add(pad('count roots'), function () {
|
||||
const res = countRoots()
|
||||
results.push(res)
|
||||
})
|
||||
.on('cycle', function (event) {
|
||||
console.log(String(event.target))
|
||||
})
|
||||
.on('complete', function () {
|
||||
})
|
||||
.run()
|
Loading…
Add table
Add a link
Reference in a new issue