import {Bench} from 'tinybench' import padRight from 'pad-right' import { formatTaskResult } from './formatTaskResult.js' 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 bench = new Bench({time: 100, iterations: 100}) .add(pad('count roots'), function () { const res = countRoots() results.push(res) }) bench.addEventListener( 'cycle', event => console.log(formatTaskResult(bench, event.task)) ) await bench.run()