aboutsummaryrefslogtreecommitdiff
path: root/benchmark/index.ts
blob: 02eee8d7a46c218cf5c51e233952ef23fd708ddf (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
56
57
58
59
60
61
62
63
64
65
import { CPU, ICPU } from '../src/cpu';
import { avrInstruction } from '../src/instruction';
import { createBenchmark } from './benchmark';
import { permutations } from './permutations';
import { instructions, executeInstruction } from './instruction-fn';

/* Approach 1: use large Uint16Array with all possible opcodes */
const instructionArray = new Uint16Array(65536);
for (let i = 0; i < instructions.length; i++) {
  const { pattern } = instructions[i];
  for (const opcode of permutations(pattern.replace(/ /g, '').substr(0, 16))) {
    if (!instructionArray[opcode]) {
      instructionArray[opcode] = i + 1;
    }
  }
}

function avrInstructionUintArray(cpu: CPU) {
  const opcode = cpu.progMem[cpu.pc];
  executeInstruction(instructionArray[opcode], cpu, opcode);
}

/* Approach 2: use instMap */
const instructionMap: { [key: number]: (cpu: ICPU, opcode: number) => void } = {};
for (const { pattern, fn } of instructions) {
  for (const opcode of permutations(pattern.replace(/ /g, '').substr(0, 16))) {
    if (!instructionMap[opcode]) {
      instructionMap[opcode] = fn;
    }
  }
}

function avrInstructionObjMap(cpu: CPU) {
  const opcode = cpu.progMem[cpu.pc];
  instructionMap[opcode](cpu, opcode);
}

/* Run the benchmark */
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();