Skip to content

Commit bea40e4

Browse files
🚀 perf: Draft for profiling code.
1 parent 070257f commit bea40e4

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

_profile/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.0x/
2+
/yarn.lock

_profile/add.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
console.time('prepare');
2+
require('@babel/polyfill');
3+
const crypto = require('crypto');
4+
const ArgumentParser = require('argparse').ArgumentParser;
5+
//const itertools = require('@aureooms/js-itertools');
6+
const XorShift128Plus = require('xorshift.js').XorShift128Plus;
7+
const { ZZ } = require('..');
8+
const BN = require('bn.js');
9+
10+
const parser = new ArgumentParser();
11+
parser.addArgument(['M'], {defaultValue: 1000, nargs: '?'});
12+
parser.addArgument(['-N'], {defaultValue: 1000});
13+
parser.addArgument(['-s'], {defaultValue: process.env.SEED || crypto.randomBytes(16).toString('hex')});
14+
const args = parser.parseArgs();
15+
const M = args.M;
16+
const N = args.N;
17+
const seed = args.s;
18+
19+
console.log('operand size (bytes):', M);
20+
console.log('number of operations:', N);
21+
console.log('seed:', seed);
22+
23+
const prng = new XorShift128Plus(seed);
24+
const _x = prng.randomBytes(M).toString('hex');
25+
console.log('_x:', _x);
26+
const _y = prng.randomBytes(M).toString('hex');
27+
console.log('_y:', _y);
28+
29+
let x = ZZ.from(_x, 16);
30+
const y = ZZ.from(_y, 16);
31+
//let x = BigInt('0x'+_x) ;
32+
//const y = BigInt('0x'+_y) ;
33+
//let x = new BN(_x,16) ;
34+
//const y = new BN(_y,16) ;
35+
36+
console.timeEnd('prepare');
37+
38+
console.time('loop');
39+
for (let k = 0; k < N; ++k) {
40+
x = x.mul(y);
41+
//x *= y;
42+
//x = x.add(y);
43+
//x = x.sub(y);
44+
//x = x + y;
45+
//x = x - y;
46+
}
47+
console.timeEnd('loop');
48+
49+
if (Math.random() < 0.0001) console.log(x);

_profile/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "_profile",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"profile": "0x"
8+
},
9+
"dependencies": {
10+
"0x": "^4.9.1",
11+
"@babel/polyfill": "^7.8.7",
12+
"argparse": "^1.0.10",
13+
"bn.js": "^5.1.1",
14+
"crypto": "^1.0.1",
15+
"xorshift.js": "^1.0.5"
16+
}
17+
}

0 commit comments

Comments
 (0)