Skip to content

Instantly share code, notes, and snippets.

@BruJu
Last active December 4, 2020 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruJu/8978c2fbd4ca361a6411abbcf0a8d283 to your computer and use it in GitHub Desktop.
Save BruJu/8978c2fbd4ca361a6411abbcf0a8d283 to your computer and use it in GitHub Desktop.
forEachEvaluation
import json
import numpy
with open('forEachEval.json') as json_file:
data = json.load(json_file)
def avg(l):
s = sum(l)
return s / len(l)
for k in data:
l = data[k]
print("{} : avg = {} ; min = {}, max = {}, std = {}".format(k, avg(l), min(l), max(l), numpy.std(l)))
import json
import numpy
with open('forEachEval.json') as json_file:
data = json.load(json_file)
def avg(l):
s = sum(l)
return s / len(l)
for k in data:
l = data[k]
print("{} : avg = {} ; min = {}, max = {}, std = {}".format(k, avg(l), min(l), max(l), numpy.std(l)))
classicLoop : avg = 69.94 ; min = 62, max = 95, std = 5.700561375864662
arrayIterator : avg = 121.54 ; min = 102, max = 156, std = 11.03487199744519
almostSafeIterator : avg = 96.79 ; min = 85, max = 122, std = 6.917073080429323
forEach : avg = 65.04 ; min = 58, max = 91, std = 5.907486775270851
forEachNoLeak : avg = 96.04 ; min = 87, max = 114, std = 6.302253565193962
let countMethods = [
["classicLoop", function (dataset) {
let n = 0;
for (let _ of dataset.getIteratorExperiment(2)) {
++n;
}
return n;
}],
["arrayIterator", function (dataset) {
let n = 0;
for (let _ of dataset.getIteratorExperiment(1)) {
++n;
}
return n;
}],
["almostSafeIterator", function (dataset) {
let n = 0;
for (let _ of dataset.getIteratorExperiment(3)) {
++n;
}
return n;
}],
["forEach", function (dataset) {
let n = 0;
dataset.forEachExperiment(_ => { ++n; }, 1);
return n;
}],
["forEachNoLeak", function (dataset) {
let n = 0;
dataset.forEachExperiment(_ => { ++n; }, 2);
return n;
}]
];

100 measures per method, 13000 triples

{"classicLoop":[68,64,66,63,64,65,63,65,62,65,65,95,87,90,73,70,67,67,70,69,69,70,68,69,72,69,69,68,77,77,72,76,69,73,63,66,64,65,72,67,72,65,67,68,72,72,71,78,65,75,70,74,66,73,69,66,66,66,64,69,68,65,66,76,66,66,67,68,69,68,67,66,73,75,77,75,67,66,69,65,64,68,69,77,74,67,65,72,67,72,73,68,68,75,72,74,74,68,79,88],"arrayIterator":[156,149,109,118,131,116,129,111,110,103,128,112,102,111,107,146,140,137,116,115,141,119,115,122,111,117,129,128,131,114,130,126,117,127,125,110,109,113,112,104,124,113,122,124,112,110,128,117,136,117,109,134,114,114,122,112,104,124,125,114,131,131,118,138,110,116,133,135,123,124,114,148,118,117,118,112,124,133,138,105,112,109,118,116,134,127,114,125,131,130,134,112,114,127,114,116,133,131,118,132],"almostSafeIterator":[95,107,88,88,114,122,90,92,93,89,97,88,88,92,85,88,97,111,96,97,95,97,96,95,95,97,96,96,97,116,117,94,96,103,89,92,106,100,89,96,91,101,93,100,99,94,97,94,90,101,105,89,91,101,93,96,90,90,91,94,96,94,109,106,100,99,96,105,94,106,94,94,91,92,91,92,103,95,90,103,102,104,91,91,90,95,96,107,100,92,99,94,107,93,91,94,100,100,106,98],"forEach":[61,60,60,60,60,58,59,58,74,58,60,59,58,59,59,62,64,64,79,85,81,67,67,70,63,75,63,65,61,65,76,62,66,58,62,60,66,73,64,65,65,64,60,61,70,72,58,61,64,65,60,62,61,61,64,69,63,79,66,67,62,68,66,67,61,62,63,65,63,63,63,62,67,62,63,76,62,66,64,74,67,62,67,91,61,62,62,62,68,61,64,69,70,66,67,64,65,66,62,71],"forEachNoLeak":[96,99,93,92,88,88,94,91,89,88,114,113,110,87,89,90,89,88,96,96,96,97,107,96,96,97,96,92,99,97,106,102,89,95,95,98,105,89,89,106,90,96,96,105,112,96,94,94,89,93,91,88,100,94,113,105,96,97,94,96,96,94,92,91,97,107,113,91,100,92,97,94,91,92,91,95,94,96,88,107,94,91,94,95,100,94,98,97,91,94,98,92,103,95,96,94,90,92,101,101]}
/**
*
* @param {*} quadRunIteratee
* @param {*} testCase 1 = default, 2 = memory friendly
*/
forEachExperiment(quadRunIteratee, testCase) {
if (testCase == 1) {
this.base.forEach(quadRunIteratee);
} else if (testCase == 2) {
const f = function (wasm_quad) {
const js_quad = rebuild_quad(wasm_quad);
wasm_quad.free();
return quadRunIteratee(js_quad);
}
this.base.forEach(f);
} else {
return false;
}
return true;
}
/**
*
* @param {*} testCase 1 = array iterator, 2 = getIterator, 3 = getSafeIterator
*/
getIteratorExperiment(testCase) {
let base = this.base;
switch (testCase) {
case 1:
return this.toArray();
case 2:
return {
base: base,
[Symbol.iterator]() {
return this.base.getIterator();
}
};
case 3:
return {
base: base,
[Symbol.iterator]() {
return {
iter: this.base.getIterator(),
next() {
if (this.iter === undefined) {
return { value: undefined, done: true };
}
let wasm_next = this.iter.next();
let done = wasm_next.done;
if (!done) {
let wasm_quad = wasm_next.value;
const js_quad = rebuild_quad(wasm_quad);
wasm_quad.free();
wasm_next.free();
return { value: js_quad, done: false };
} else {
wasm_next.free();
this.iter.free();
this.iter = undefined;
return { value: undefined, done: true };
}
}
};
}
};
default:
return undefined;
}
}
@BruJu
Copy link
Author

BruJu commented Jul 13, 2020

100k triples, Wasm Tree Loop vs Sophia Tree

{"wasmTreeLoop":[22,14,13,11,14,12,13,13,12,13]}	

{"classicLoop":[186,224,167,145,133,141,136,187,137,137],
"arrayIterator":[730,749,589,753,617,594,658,596,586,678],
"almostSafeIterator":[307,199,194,185,202,203,278,212,244,273],
"forEach":[150,148,241,207,174,274,152,153,149,261],
"forEachNoLeak":[319,199,210,236,214,213,207,245,352,245]}

@BruJu
Copy link
Author

BruJu commented Jul 15, 2020

wasmTreeLoop : 13ms, Wasm Tree implementation (get a list of numbers, convert it on the fly to quads)

classicLoop : export an iterator from wasm to js that serves raw quads, 159.3 ms
almostSafeIterator : export an iterator from wasm to js, but rewrap the served quad and iterators to free the memory : 229.7 ms
forEach : dataset.forEach(function) where the user pass the function, 190.9 ms
forEachNoLeak : dataset.forEach(new_function) where new_function transforms wasm quads to n3 quads, 244ms
arrayIterator : return a NQuads text, that is parsed by n3.Parser to an array, and iterate on this array : 655ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment