-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtest.js
76 lines (67 loc) · 2.41 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const Spline = require("./");
const test = require("tape");
test("spline", function (t) {
const xs = [1, 2, 3, 4, 5];
const ys = [9, 3, 6, 2, 4];
// new a Spline object
const spline = new Spline(xs, ys);
// get Y at arbitrary X
t.equal(spline.at(1.4), 5.586);
// interpolate a line at a higher resolution
for (var i = 0; i < 50; i++) {
t.equal(typeof spline.at(i * 0.1), "number");
}
t.end();
});
test("extrapolate outside bounds", function (t) {
const xs = [0, 1];
const ys = [0, 1];
const spline = new Spline(xs, ys);
// In practice you would only want to do this slightly outside the bounds
t.equal(spline.at(-1), -1);
t.equal(spline.at(2), 2);
t.end();
});
test("speed", function (t) {
let arraySizes = [10, 100, 1000, 2000, 4000];
let arraySpeed = [];
let arrayInitialization = [];
let multiplicationFactor = 10; // array.length * multiplicationFactor = number of times the .at()-function is executed on the same array
for (let i = 0; i < arraySizes.length; i++) {
// generate x-y-set
let setX = [];
let setY = [];
let x = 0;
let y = 0;
for (let j = 0; j < arraySizes[i]; j++) {
x += 0.05;
y = Math.sin(x);
setX.push(x.toFixed(2));
setY.push(y.toFixed(4));
}
// run cubic-spline-interpolation
let initializationStart = new Date();
const spline = new Spline(setX, setY);
let initializationEnd = new Date();
// interpolate spline at 10x resolution
let atStart = new Date();
for (let j = 0; j < arraySizes[i] * multiplicationFactor; j++) {
spline.at(j * 0.05 + 0.01); // create a small offset to the actual data set
}
let atEnd = new Date();
arrayInitialization[i] = initializationEnd - initializationStart;
arraySpeed[i] = atEnd - atStart;
}
// print result
console.log(`elements in | times .at() | initialization | execution time`);
console.log(`dataset | is used | time of spline | of all .at()'s`);
console.log(`------------|-------------|----------------|---------------`);
for (let i = 0; i < arraySizes.length; i++) {
let size = arraySizes[i].toString().padEnd(11);
let at = (arraySizes[i] * multiplicationFactor).toString().padStart(11);
let initializationTime = arrayInitialization[i].toString().padStart(12);
let atTime = arraySpeed[i].toString().padStart(12);
console.log(`${size} | ${at} | ${initializationTime}ms | ${atTime}ms`);
}
t.end();
});