Skip to content

Commit

Permalink
Marginal improvements in benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgodbolt committed Aug 10, 2024
1 parent dab90c6 commit 7986ef9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
12 changes: 12 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions 6502.opcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,20 @@ function makeCpuFunctions(cpu, opcodes, is65c12) {

function generate6502JumpTable() {
const funcs = [];
for (let opcode = 0; opcode < 256; ++opcode) {
funcs[opcode] = new Function("cpu", getIndentedSource(" ", opcode, true));
for (let opcodeNum = 0; opcodeNum < 256; ++opcodeNum) {
const opcodeFunc = new Function("cpu", getIndentedSource(" ", opcodeNum, true));
let funcName = `exec_${utils.hexbyte(opcodeNum)}_`;
if (opcodes[opcodeNum]) {
const instrName = opcodes[opcodeNum]
.replace("()", "ind")
.replace("(,x)", "ind_x")
.replace("(abs)", "ind_abs")
.replace(/[^A-Za-z0-9]/g, "_")
.toLowerCase();
funcName += instrName;
} else funcName += "undef";
Object.defineProperty(opcodeFunc, "name", { writable: true, value: funcName });
funcs[opcodeNum] = opcodeFunc;
}
return function exec(opcode) {
return funcs[opcode].call(this, this.cpu);
Expand Down
24 changes: 13 additions & 11 deletions app-bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import { fake6502 } from "./fake6502.js";
import * as models from "./models.js";
import * as disc from "./fdc.js";

function benchmarkCpu(cpu, numCycles) {
numCycles = numCycles || 10 * 1000 * 1000;
console.log("Benchmarking over " + numCycles + " cpu cycles");
const startTime = Date.now();
cpu.execute(numCycles);
const endTime = Date.now();
const msTaken = endTime - startTime;
const virtualMhz = numCycles / msTaken / 1000;
console.log("Took " + msTaken + "ms to execute " + numCycles + " cycles");
console.log("Virtual " + virtualMhz.toFixed(2) + "MHz");
function benchmarkCpu(cpu, numCyclesPerRep, numTimes) {
console.log(`Benchmarking over ${numCyclesPerRep / 1000000}M cpu cycles, ${numTimes} times`);
for (let time = 0; time < numTimes; ++time) {
const startTime = Date.now();
cpu.execute(numCyclesPerRep);
const endTime = Date.now();
const msTaken = endTime - startTime;
const virtualMhz = numCyclesPerRep / msTaken / 1000;
console.log(
`${time}: Took ${msTaken}ms to execute ${numCyclesPerRep / 1000000}M cycles; Virtual ${virtualMhz.toFixed(2)}MHz`,
);
}
}

async function main() {
Expand All @@ -25,7 +27,7 @@ async function main() {
cpu.sysvia.keyDown(16);
cpu.execute(10 * 1000 * 1000);
cpu.sysvia.keyUp(16);
benchmarkCpu(cpu, 100 * 1000 * 1000);
benchmarkCpu(cpu, 100 * 1000 * 1000, 10);
}

main().then(() => {});

0 comments on commit 7986ef9

Please sign in to comment.