aboutsummaryrefslogtreecommitdiff
path: root/benchmark/index.ts
blob: b90b6bd30cb416cddd223cb33511c9cfc436ef5c (plain)
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
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<number, (cpu: CPU, opcode: number) => 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();