Skip to content

Commit

Permalink
timing code
Browse files Browse the repository at this point in the history
  • Loading branch information
ixchow committed Aug 7, 2020
1 parent 627962c commit 5277820
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CellMachine.js
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,8 @@ CellMachine.prototype.miss = function CellMachine_miss(d, n, cs) {

CellMachine.prototype.pause = function CellMachine_pause() { /* nothing */ };

CellMachine.prototype['x-stitch-number'] = function Cellmachine_x_stitch_number(args) { };

CellMachine.prototype['x-vis-color'] = function Cellmachine_x_vis_color(args) {
let toks = Array.from(arguments);
let color = toks.shift();
Expand Down
2 changes: 1 addition & 1 deletion evalJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ function evalJS(codeText){

let captureConsole = {
log: function() {
console.log.apply(console, arguments);
//console.log.apply(console, arguments);
let str = '';
for (let i = 0; i < arguments.length; ++i) {
if (i !== 0) str += ' ';
Expand Down
6 changes: 3 additions & 3 deletions parseKnitout.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ function parseKnitout(codeText, machine, useKnitoutAsSource=false) {
} else {
var name = m[1];
var value = m[2];
console.log("Comment header: '" + name + "' is '" + value + "'.");
//TODO: handle comment headers.
//console.log("Comment header: '" + name + "' is '" + value + "'.");
if (name === "Carriers") {
carrierNames = value.split(" ");
machine.setCarriers(carrierNames);
console.log("Carrier names (front-to-back): ", carrierNames);
//console.log("Carrier names (front-to-back): ", carrierNames);
}
//TODO: handle other comment headers.
return; //nothing left to do with this line.
}
} else {
Expand Down
82 changes: 82 additions & 0 deletions visualizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,87 @@
show.showKnitout.parse(knitout, false, centerView);
}

//Timing code:
if (false) {
function TimingMachine() {
this.count = 0;
this.ignored = 0;
}
function countOp() { this.count += 1; }
function ignoreOp() { this.ignored += 1; }
["in","out","inhook","releasehook","outhook","stitch","rack","pause","x-stitch-number"].forEach(function(op) {
TimingMachine.prototype[op] = ignoreOp;
});
["knit","tuck","split","miss"].forEach(function(op) {
TimingMachine.prototype[op] = countOp;
});
TimingMachine.prototype.setCarriers = function() { }
TimingMachine.prototype.stretchLoops = function() { }

const oldUpdateVisualizer = updateVisualizer;
updateVisualizer = function timeUpdateVisualizer(centerView) {
const TIMING_ITERS = 100;

//time code generation:
let codeParse = {
min:Infinity,
total:0.0,
boxInstructions:NaN,
otherInstructions:NaN
};
(() => {
for (let iter = 0; iter < TIMING_ITERS; ++iter) {
let before = performance.now();
updateKnitoutMode();

let code = editor.getValue();
if (isKnitout()) {
//mark up code with line numbers:
knitout = '';
code.split('\n').forEach(function(line, lineNumber){
knitout += line;
if (line.indexOf(';') === -1) {
knitout += ';!source:' + (lineNumber+1);
}
knitout += '\n';
});
} else {
knitout = evalJS(code);
}

const tm = new TimingMachine();
parseKnitout(knitout, tm, false);
let after = performance.now();
codeParse.min = Math.min(codeParse.min, after - before);
codeParse.total += (after - before);
if (codeParse.boxInstructions === codeParse.boxInstructions) {
console.assert(tm.count === codeParse.boxInstructions);
console.assert(tm.ignored === codeParse.otherInstructions);
}
codeParse.boxInstructions = tm.count;
codeParse.otherInstructions = tm.ignored;
}
})();

//time visualization overall:
(() => {
let min = Infinity;
let total = 0.0;
for (let iter = 0; iter < TIMING_ITERS; ++iter) {
let before = performance.now();
oldUpdateVisualizer(centerView);
let after = performance.now();
min = Math.min(min, after - before);
total += (after - before);
}
console.log("Had " + codeParse.boxInstructions + " box-generating instructions and " + codeParse.otherInstructions + " other instructions; user code + parse took " + codeParse.min + " ms min / " + codeParse.total / TIMING_ITERS + " ms avg over " + TIMING_ITERS + " runs.");
console.log("Update took " + min + " ms min, " + (total / TIMING_ITERS) + " ms avg over " + TIMING_ITERS + " iterations.");
console.log(" User/source: " + codeParse.min + " ms min ; vis: " + (min - codeParse.min) + " ms min; vis/ins: " + (min - codeParse.min) / codeParse.boxInstructions + " ms / instruction min.");
//codeParse.total / TIMING_ITERS + " ms avg over " + TIMING_ITERS + " runs.");
//console.log("Update took " + min + " ms min, " + (total / TIMING_ITERS) + " ms avg over " + TIMING_ITERS + " iterations.");
})();
};
}


updateVisualizer(true);
Expand Down Expand Up @@ -335,6 +416,7 @@
console.warn("Converted dos-style line endings to unix-style.")
}
editor.setValue(text, -1);

updateVisualizer(!sameFile);
};
console.log("reading " + file.name);
Expand Down

0 comments on commit 5277820

Please sign in to comment.