function stringBench(n, bench, min = 10, len = 10, logDif = false) {
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
var a = [];
var b = [];
var pool = {};
let rle = [];
let rlc = [];
let now;
for (let i = 0; i < n; i++) {
pool[i] = (makeid(min + Math.floor(Math.random() *
len))); //10-20ish
}
for (let i = 0; i < bench; i++) {
a[i] = (Math.floor(Math.random() * n));
b[i] = (Math.floor(Math.random() * n));
}
console.log("now testin le vs lc on a pool of", n, " with this many samples ", bench);
now = Date.now();
for (let i = 0; i < bench; i++) {
rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
}
let lcDelta = Date.now() - now;
console.log("Performed ", bench, "lc localeCompare in ", lcDelta);
now = Date.now();
for (let i = 0; i < bench; i++) {
rle[i] = pool[a[i]] <= pool[b[i]];
}
let leDelta = Date.now() - now;
console.log("Performed ", bench, "le (<=) compares in ", leDelta)
for (let i = 0; i < n; i++) {
pool[i] = (makeid(min + Math.floor(Math.random() *
len))); //10-20ish
}
for (let i = 0; i < bench; i++) {
a[i] = (Math.floor(Math.random() * n));
b[i] = (Math.floor(Math.random() * n));
}
now = Date.now();
for (let i = 0; i < bench; i++) {
rle[i] = pool[a[i]] <= pool[b[i]];
}
let leDelta2 = Date.now() - now;
console.log("Performed ", bench, "le (<=) compares in ", leDelta2)
now = Date.now();
for (let i = 0; i < bench; i++) {
rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
}
let lcDelta2 = Date.now() - now;
console.log("Performed ", bench, "lc localeCompare in ", lcDelta2);
function testCmp(a, b, log = true) {
let le = a <= b;
let ge = a >= b;
let lc = a.localeCompare(b);
let l = a < b;
let g = a > b;
if (le && ge) console.assert(lc == 0, 'le && ge -> == -> lc == 0,')
if (le) console.assert(lc <= 0, 'le-> lc <= 0')
if (ge) console.assert(lc >= 0, 'ge-> lc >= 0')
if (l) console.assert(lc < 0, 'l=>lc < 0')
if (g) console.assert(lc > 0, 'g-> lc > 0')
if (!log) return;
console.log(`Compare: ${a} le ${b} `, a <= b);
console.log(`Compare: ${a} ge ${b}`, a >= b);
console.log(`Compare: ${a} lc ${b}`, a.localeCompare(b));
}
let c = 0
for (let i = 0; i < bench; i++) {
if (rle[i] != rlc[i] <= 0) {
c++;
testCmp(pool[a[i]], pool[b[i]], true);
console.warn(pool[a[i]], ' le != lc <= 0 ', pool[b[i]]);
}
// rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
}
console.warn(' le != lc out of bench, num diffs: ', c);
testCmp('ff', 'ff')
testCmp('ff', 'fa')
testCmp('ff', 'fz')
testCmp('ff', 'fff')
testCmp('ff', 'ffa')
testCmp('ff', 'ffz')
testCmp('ff', 'a')
testCmp('ff', 'z')
testCmp('ff', 'f')
testCmp('ff', 'zff')
testCmp('ff', 'aff')
testCmp('ff', 'ZZ')
testCmp('ff', 'AA')
testCmp('FF', 'ZZ')
testCmp('FF', 'ff')
testCmp('FF', 'AA')
testCmp('ff', 'ZZZ')
console.log("Dif le - lc = ", leDelta2 - lcDelta2);
console.log("avg le ms/Mops = ", (leDelta + leDelta2) / (bench / 1000000));
console.log("avg lc ms/Mops = ", (lcDelta + lcDelta2) / (bench / 1000000));
console.log("Dif - lc = ", leDelta2 - lcDelta2);
};
stringBench(1000, 5000, 1, 3, true);
// stringBench(1000000, 1000000);//nothing is equire
// stringBench(1000, 100000000);
// stringBench(1000000, 100000000, 3, 5);
// stringBench(1000000, 100000000, 15, 20);
function stringBench(n, bench, min = 10, len = 10, logDif = false) {
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}
var a = [];
var b = [];
var pool = {};
let rle = [];
let rlc = [];
let now;
for (let i = 0; i < n; i++) {
pool[i] = (makeid(min + Math.floor(Math.random() *
len))); //10-20ish
}
for (let i = 0; i < bench; i++) {
a[i] = (Math.floor(Math.random() * n));
b[i] = (Math.floor(Math.random() * n));
}
console.log("now testin le vs lc on a pool of", n, " with this many samples ", bench);
now = Date.now();
for (let i = 0; i < bench; i++) {
rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
}
let lcDelta = Date.now() - now;
console.log("Performed ", bench, "lc localeCompare in ", lcDelta);
now = Date.now();
for (let i = 0; i < bench; i++) {
rle[i] = pool[a[i]] <= pool[b[i]];
}
let leDelta = Date.now() - now;
console.log("Performed ", bench, "le (<=) compares in ", leDelta)
for (let i = 0; i < n; i++) {
pool[i] = (makeid(min + Math.floor(Math.random() *
len))); //10-20ish
}
for (let i = 0; i < bench; i++) {
a[i] = (Math.floor(Math.random() * n));
b[i] = (Math.floor(Math.random() * n));
}
now = Date.now();
for (let i = 0; i < bench; i++) {
rle[i] = pool[a[i]] <= pool[b[i]];
}
let leDelta2 = Date.now() - now;
console.log("Performed ", bench, "le (<=) compares in ", leDelta2)
now = Date.now();
for (let i = 0; i < bench; i++) {
rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
}
let lcDelta2 = Date.now() - now;
console.log("Performed ", bench, "lc localeCompare in ", lcDelta2);
function testCmp(a, b, log = true) {
let le = a <= b;
let ge = a >= b;
let lc = a.localeCompare(b);
let l = a < b;
let g = a > b;
if (le && ge) console.assert(lc == 0, 'le && ge -> == -> lc == 0,')
if (le) console.assert(lc <= 0, 'le-> lc <= 0')
if (ge) console.assert(lc >= 0, 'ge-> lc >= 0')
if (l) console.assert(lc < 0, 'l=>lc < 0')
if (g) console.assert(lc > 0, 'g-> lc > 0')
if (!log) return;
console.log(`Compare: ${a} le ${b} `, a <= b);
console.log(`Compare: ${a} ge ${b}`, a >= b);
console.log(`Compare: ${a} lc ${b}`, a.localeCompare(b));
}
let c = 0
for (let i = 0; i < bench; i++) {
if (rle[i] != rlc[i] <= 0) {
c++;
testCmp(pool[a[i]], pool[b[i]], true);
console.warn(pool[a[i]], ' le != lc <= 0 ', pool[b[i]]);
}
// rlc[i] = pool[a[i]].localeCompare(pool[b[i]]);
}
console.warn(' le != lc out of bench, num diffs: ', c);
testCmp('ff', 'fa')
testCmp('ff', 'fz')
testCmp('ff', 'ZZ')
console.log("Dif le - lc = ", leDelta2 - lcDelta2);
console.log("avg le ms/Mops = ", (leDelta + leDelta2) / (bench / 1000000));
console.log("avg lc ms/Mops = ", (lcDelta + lcDelta2) / (bench / 1000000));
console.log("Dif - lc = ", leDelta2 - lcDelta2);
// for (let i = 0; i < bench; i++) {
// rlc[i] != rle[i]
// pool[a[i]].localeCompare(pool[b[i]]);
// }
//
// console.log(makeid(5));
};
stringBench(1000, 5000, 1, 3, true);
// stringBench(1000000, 1000000);//nothing is equire
// stringBench(1000, 100000000);
// stringBench(1000000, 100000000, 3, 5);
// stringBench(1000000, 100000000, 15, 20);