nv-cli-sort
v1.0.5
Published
nv-cli-sort ========== - cli tool, generate a simple-sort-code for variables IF variable-count <=6 - the generated stupid sort-function is 5~10 times faster than built-in sort IF elements <= 8 - suitable for params sort
Downloads
4
Readme
nv-cli-sort
- cli tool, generate a simple-sort-code for variables IF variable-count <=6
- the generated stupid sort-function is 5~10 times faster than built-in sort IF elements <= 8
- suitable for params sort
install
- npm install nv-cli-sort -g
after install .
your can delete the files in ./TEST, execpt gen.js(which is used for generate test-code);
those are big and USELESS
usage
sort
Usage: nv_cli_sort [options]
Options:
-v, --var_names variable names
-o, --using_lt_oper compare function using <, this is default mode
-f, --using_lt_func compare function using fname = (a,b)=>boolean IF true MEANS a<b
-s, --using_sort_func compare function using fname = (a,b)=>-1|0|1
-n, --sort_func_name sort function name, default "func"
-i, --indent indent count default 4
-h, --help usage
swap
Usage: nv_cli_swap [options]
Options:
-f, --fst first var name
-s, --snd second var name
-i, --indent indent count default 4
-h, --help usage
mv
Usage: nv_cli_mv [options]
Options:
-f, --fst first var name
-s, --snd second var name
-n, --empty nul default undefined
-i, --indent indent count default 4
-h, --help usage
example
sort IT will generate a code segment, JUST copy it to your code
# nv_cli_sort -v my_var your_var his_var her_var
{
let _tmp_ ;
if ( your_var < my_var ) {
_tmp_ = your_var;
your_var = my_var ;
my_var = _tmp_ ;
} else {
}
if ( his_var < my_var ) {
_tmp_ = his_var ;
his_var = your_var;
your_var = my_var ;
my_var = _tmp_ ;
} else if ( his_var < your_var ) {
_tmp_ = his_var ;
his_var = your_var;
your_var = _tmp_ ;
} else {
}
if ( her_var < my_var ) {
_tmp_ = her_var ;
her_var = his_var ;
his_var = your_var;
your_var = my_var ;
my_var = _tmp_ ;
} else if ( her_var < your_var ) {
_tmp_ = her_var ;
her_var = his_var ;
his_var = your_var;
your_var = _tmp_ ;
} else if ( her_var < his_var ) {
_tmp_ = her_var ;
her_var = his_var ;
his_var = _tmp_ ;
} else {
}
}
swap
# nv_cli_swap -f var0 -s var1
{
let _tmp_ = var0 ;
var0 = var1 ;
var1 = _tmp_ ;
_tmp_ = undefined ;
}
#
mv
# nv_cli_mv -f src -s dst
{
dst = src ;
src = undefined ;
}
#
test
two params AND three params SIMILIAR TO foue params
four params
const {sync} = require("nv-facutil-simple-test");
const swap = (ary,i,j)=> {
let tmp = ary[i];
ary[i] = ary[j];
ary[j] = tmp
}
const rand_int32 =(a,b)=>~~(Math.random()*(b-a))+a;
const shuffle_inplace = (ary)=> {
for(let i=ary.length-1;i>0;i--) {
const idx = rand_int32(0,i);
swap(ary,i,idx)
}
return(ary)
}
var SAMPLES = []
for(let i=0;i<1000000;++i) {SAMPLES.push(i)};
shuffle_inplace(SAMPLES);
////---------------------------
const sort_func = (a,b)=>a-b;
const f0 = (v0,v1,v2,v3) => {
let a = [v0,v1,v2,v3];
a.sort(sort_func);
return(a)
}
const f1 = (v0,v1,v2,v3) => {
{
let _tmp_;
if ( v1 < v0 ) {
_tmp_ = v1 ;
v1 = v0 ;
v0 = _tmp_;
} else {
}
if ( v2 < v0 ) {
_tmp_ = v2 ;
v2 = v1 ;
v1 = v0 ;
v0 = _tmp_;
} else if ( v2 < v1 ) {
_tmp_ = v2 ;
v2 = v1 ;
v1 = _tmp_;
} else {
}
if ( v3 < v0 ) {
_tmp_ = v3 ;
v3 = v2 ;
v2 = v1 ;
v1 = v0 ;
v0 = _tmp_;
} else if ( v3 < v1 ) {
_tmp_ = v3 ;
v3 = v2 ;
v2 = v1 ;
v1 = _tmp_;
} else if ( v3 < v2 ) {
_tmp_ = v3 ;
v3 = v2 ;
v2 = _tmp_;
} else {
}
}
return([v0,v1,v2,v3])
}
/////
const {deepStrictEqual} = require("assert");
for(let i=0;i<SAMPLES.length;i=i+4) {
let r0 = f0(SAMPLES[i], SAMPLES[i+1], SAMPLES[i+2], SAMPLES[i+3]);
let r1 = f1(SAMPLES[i], SAMPLES[i+1], SAMPLES[i+2], SAMPLES[i+3]);
deepStrictEqual(r0,r1);
}
var RTRN;
var a = SAMPLES.slice(0,4)
const ff0 = ()=> {RTRN = f0(a[0],a[1],a[2],a[3])}
const ff1 = ()=> {RTRN = f1(a[0],a[1],a[2],a[4])}
console.log(sync(1000000,ff0))
console.log(sync(1000000,ff1))
/*
{ rounds: 1000000, f: [Function: ff0], costed: 247.4128758907318 }
{ rounds: 1000000, f: [Function: ff1], costed: 29.41348898410797 }
*/
const fff0 = ()=> {
for(let i=0;i<SAMPLES.length;i=i+4) {
RTRN = f0(SAMPLES[i], SAMPLES[i+1], SAMPLES[i+2], SAMPLES[i+3])
}
}
const fff1 = ()=> {
for(let i=0;i<SAMPLES.length;i=i+4) {
RTRN = f1(SAMPLES[i], SAMPLES[i+1], SAMPLES[i+2], SAMPLES[i+3])
}
}
console.log(sync(100,fff0))
console.log(sync(100,fff1))
/*
{ rounds: 100, f: [Function: fff0], costed: 6598.902182936668 }
{ rounds: 100, f: [Function: fff1], costed: 789.9169020652771 }
*/
five six seven IS similar to eight
eight params
{ rounds: 1000000, f: [Function: ff0], costed: 385.5723639726639 }
{ rounds: 1000000, f: [Function: ff1], costed: 90.83020997047424 }
{ rounds: 100, f: [Function: fff0], costed: 6288.904719948769 }
{ rounds: 100, f: [Function: fff1], costed: 1128.7812089920044 }
WHEN large than 8 params ,the generated code will BE larger than 5K , its NOT good
9,10,11,12,13,14,15 is simliar to 16
16 params
{ rounds: 1000000, f: [Function: ff0], costed: 851.4200350046158 }
{ rounds: 1000000, f: [Function: ff1], costed: 97.38632500171661 }
{ rounds: 100, f: [Function: fff0], costed: 7374.491150021553 }
{ rounds: 100, f: [Function: fff1], costed: 4483.460022926331 }
large than 22 params
this trick not work
the built-in sort is faster.
{ rounds: 1000000, f: [Function: ff0], costed: 1287.3770550489426 }
{ rounds: 1000000, f: [Function: ff1], costed: 155.83846402168274 }
{ rounds: 100, f: [Function: fff0], costed: 8222.46571803093 }
{ rounds: 100, f: [Function: fff1], costed: 8569.382011055946 }
LICENSE
- ISC