From ec512ea868d5df64a8a11ed27d5032337760ffde Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Sun, 1 Dec 2019 22:04:02 +0200 Subject: feat: add benchmarking code --- benchmark/index.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 benchmark/index.ts (limited to 'benchmark/index.ts') diff --git a/benchmark/index.ts b/benchmark/index.ts new file mode 100644 index 0000000..b90b6bd --- /dev/null +++ b/benchmark/index.ts @@ -0,0 +1,55 @@ +import { CPU } from '../src/cpu'; +import { avrInstruction } from '../src/instruction'; +import { createBenchmark } from './benchmark'; +import { instLDY } from './instruction-fn'; + +/* Approach 1: use large Uint16Array with all possible opcodes */ +const instructionMap = new Uint16Array(65536); +instructionMap[0x8088] = 0x5; + +function avrInstructionUintArray(cpu: CPU) { + const opcode = cpu.progMem[cpu.pc]; + const mapped = instructionMap[opcode]; + switch (mapped) { + case 5: + instLDY(cpu, opcode); + break; + } +} + +/* Approach 1: use Map() */ +const objMap = new Map void>(); +objMap.set(0x8088, instLDY); + +function avrInstructionObjMap(cpu: CPU) { + const opcode = cpu.progMem[cpu.pc]; + objMap.get(cpu.progMem[cpu.pc])(cpu, opcode); +} + +function run() { + const benchmark = createBenchmark('cpu-benchmark'); + + const cpu = new CPU(new Uint16Array(0x1000)); + cpu.progMem[0] = 0x8088; + const timeA = benchmark('avrInstruction'); + while (timeA()) { + cpu.pc = 0; + avrInstruction(cpu); + } + + const timeB = benchmark('avrInstructionObjMap'); + while (timeB()) { + cpu.pc = 0; + avrInstructionObjMap(cpu); + } + + const timeC = benchmark('avrInstructionUintArray'); + while (timeC()) { + cpu.pc = 0; + avrInstructionUintArray(cpu); + } + + benchmark.report(); +} + +run(); -- cgit v1.2.3