Skip to content

Commit

Permalink
add rolling-average
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Jun 5, 2024
1 parent 01f18f3 commit 7feee88
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
20 changes: 20 additions & 0 deletions webgpu/resources/js/rolling-average.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// See https://webgpufundamentals.org/webgpu/lessons/webgpu-timing.html
export default class RollingAverage {
#total = 0;
#samples = [];
#cursor = 0;
#numSamples;
constructor(numSamples = 30) {
this.#numSamples = numSamples;
}
addSample(v) {
if (!Number.isNaN(v) && Number.isFinite(v)) {
this.#total += v - (this.#samples[this.#cursor] || 0);
this.#samples[this.#cursor] = v;
this.#cursor = (this.#cursor + 1) % this.#numSamples;
}
}
get() {
return this.#total / this.#samples.length;
}
}
1 change: 1 addition & 0 deletions webgpu/resources/js/timing-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function assert(cond, msg = '') {
}
}

// See https://webgpufundamentals.org/webgpu/lessons/webgpu-timing.html
export default class TimingHelper {
#canTimestamp;
#device;
Expand Down

0 comments on commit 7feee88

Please sign in to comment.