From c5a64bf5b714ee247dd93a1b93eb18f0efdf3e74 Mon Sep 17 00:00:00 2001 From: Luc Patiny Date: Thu, 9 May 2024 11:56:33 +0200 Subject: [PATCH] chore: add benchmark comments --- benchmark/{contour.js => contour.mjs} | 27 ++++++++++++++++----------- src/calculateContour.ts | 12 ++++++++---- tsconfig.json | 2 +- 3 files changed, 25 insertions(+), 16 deletions(-) rename benchmark/{contour.js => contour.mjs} (64%) diff --git a/benchmark/contour.js b/benchmark/contour.mjs similarity index 64% rename from benchmark/contour.js rename to benchmark/contour.mjs index 8383ceb..ae3df70 100644 --- a/benchmark/contour.js +++ b/benchmark/contour.mjs @@ -3,18 +3,21 @@ import { join } from 'path' import { convert } from 'jcampconverter'; -import { Conrec } from '../src/index.js'; +import { Conrec } from '../lib/index.js'; + +const __dirname = new URL('.', import.meta.url).pathname; const data = readFileSync(join(__dirname, '../src/__tests__/data/zhmbc_0.jdx'), 'utf8'); const parsed = convert(data, { noContour: true }).flatten[0]; + for (let i = 0; i < parsed.minMax.z.length; i++) { parsed.minMax.z[i] = Float64Array.from(parsed.minMax.z[i]) } const conrec = new Conrec(parsed.minMax.z); -const number = 20 +const number = 100 console.time('basic'); for (let i = 0; i < number; i++) { @@ -27,12 +30,14 @@ for (let i = 0; i < number; i++) { } console.timeEnd('basic'); -console.time('shape'); -for (let i = 0; i < number; i++) { - conrec.drawContour({ - contourDrawer: 'shape', - levels: [-100000, 100000], - timeout: 10000, - }); -} -console.timeEnd('shape'); +if (false) { + console.time('shape'); + for (let i = 0; i < number; i++) { + conrec.drawContour({ + contourDrawer: 'shape', + levels: [-100000, 100000], + timeout: 10000, + }); + } + console.timeEnd('shape'); +} \ No newline at end of file diff --git a/src/calculateContour.ts b/src/calculateContour.ts index a1c1149..a95060b 100644 --- a/src/calculateContour.ts +++ b/src/calculateContour.ts @@ -158,6 +158,7 @@ export function calculateContour( // Note that castab is arranged differently from the FORTRAN code because // Fortran and C/C++ arrays are transposed of each other, in this case // it is more tricky as castab is in 3 dimensions + // accessing matrix does not seem to be a speed problem: https://jsbench.me/qolvz19dwb/1 const castab = [ [ [0, 0, 8], @@ -180,6 +181,7 @@ export function calculateContour( for (let j = jub - 1; j >= jlb; j--) { if (timeout && Date.now() - start > timeout) { // `timeout: contour generation could not finish in less than ${timeout}ms` + // no impact on speed return true; } for (let i = ilb; i < iub; i++) { @@ -202,10 +204,11 @@ export function calculateContour( min2 = di1j; max2 = di1j1; } - // let dmin = Math.min(min1, min2); - // let dmax = Math.max(max1, max2); - let dmin = min1 > min2 ? min2 : min1; - let dmax = max1 > max2 ? max1 : max2; + let dmin = Math.min(min1, min2); + let dmax = Math.max(max1, max2); + // Ternary operator is now much slower: https://jsbench.me/d5l0dh502g + //let dmin = min1 > min2 ? min2 : min1; + //let dmax = max1 > max2 ? max1 : max2; if (dmax >= z0 && dmin <= znc1) { for (let k = 0; k < nc; k++) { if (z[k] >= dmin && z[k] <= dmax) { @@ -329,6 +332,7 @@ export function calculateContour( } // Put your processing code here and comment out the printf // printf("%f %f %f %f %f\n",x1,y1,x2,y2,z[k]); + // the following step takes negligibly amount of time contourDrawer.drawContour(x1, y1, x2, y2, z[k], k); } } diff --git a/tsconfig.json b/tsconfig.json index 4ed6e69..4a1e339 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "esModuleInterop": true, - "moduleResolution": "node16", + "moduleResolution": "node", "outDir": "lib", "sourceMap": true, "strict": true,