diff options
| author | Uri Shaked | 2019-12-01 23:55:42 +0200 |
|---|---|---|
| committer | Uri Shaked | 2019-12-01 23:55:42 +0200 |
| commit | 18e561d89d4065c3372da1a1d66d36e3b14a78be (patch) | |
| tree | 3857da77fc73a4788f9b34e4e56a030119b4b96c /demo/src/cpu-performance.ts | |
| parent | feat: improve benchmark code (diff) | |
| download | avr8js-18e561d89d4065c3372da1a1d66d36e3b14a78be.tar.gz avr8js-18e561d89d4065c3372da1a1d66d36e3b14a78be.tar.bz2 avr8js-18e561d89d4065c3372da1a1d66d36e3b14a78be.zip | |
feat(demo): show simulation speed
Diffstat (limited to 'demo/src/cpu-performance.ts')
| -rw-r--r-- | demo/src/cpu-performance.ts | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/demo/src/cpu-performance.ts b/demo/src/cpu-performance.ts new file mode 100644 index 0000000..f61d038 --- /dev/null +++ b/demo/src/cpu-performance.ts @@ -0,0 +1,33 @@ +import { ICPU } from 'avr8js'; + +export class CPUPerformance { + private prevTime = 0; + private prevCycles = 0; + private samples = new Float32Array(64); + private sampleIndex = 0; + + constructor(private cpu: ICPU, private MHZ: number) {} + + reset() { + this.prevTime = 0; + this.prevCycles = 0; + this.sampleIndex = 0; + } + + update() { + if (this.prevTime) { + const delta = performance.now() - this.prevTime; + const deltaCycles = this.cpu.cycles - this.prevCycles; + const deltaCpuMillis = 1000 * (deltaCycles / this.MHZ); + const factor = deltaCpuMillis / delta; + if (!this.sampleIndex) { + this.samples.fill(factor); + } + this.samples[this.sampleIndex++ % this.samples.length] = factor; + } + this.prevCycles = this.cpu.cycles; + this.prevTime = performance.now(); + const avg = this.samples.reduce((x, y) => x + y) / this.samples.length; + return avg; + } +} |
