Skip to content

Commit

Permalink
chore: add benchmark comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lpatiny committed May 9, 2024
1 parent 295b415 commit c5a64bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
27 changes: 16 additions & 11 deletions benchmark/contour.js → benchmark/contour.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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');
}
12 changes: 8 additions & 4 deletions src/calculateContour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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++) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "node16",
"moduleResolution": "node",
"outDir": "lib",
"sourceMap": true,
"strict": true,
Expand Down

0 comments on commit c5a64bf

Please sign in to comment.