User:Dan Bron/Snippets/VerbMetrics

From J Wiki
Jump to navigation Jump to search

Compare different, theoretically identical verbs. Good for running contests or discovering which algorithm best suits the data. No documentation, poor style.

Specifically slapped together for the 2 f 3 f 4 f m thread on the forum .

require 'strings numeric'

NB.  Mesaure time of execution of a sentence
time          =:  6!:2
NB.  Measure memory usage of execution of a sentence
space         =:  7!:2
NB.  Give time & spece measurements for execution of a sentence.
ts            =:  time , space@:]

TIME_TEMPLATE =: verb define
   z=.$~0
   q =. count__ ts :: ((13!:11 ,&:< 13!:12) bind '') 'z =. NAME__ y'
   z ,&<  q
:
   z=.$~0
   q =. count__ ts :: ((13!:11 ,&:< 13!:12) bind '') 'z =. x NAME__ y'
   z ,&:<  q
)

9!:41 0: retain =. 9!:40 ''
EXPL_VERB_TEMPLATE =: (verb define) (adverb define)
   5!:1{.;:'u'
)
   SMURF__  NB.  Will be replaced with TIME_TEMPLATE
)
9!:41 retain

H       =:  conjunction define
   verb_bodies =. LF >@:}:@:}.@:cut each (<'count__';'(',(5!:5{.;:'m'),')') stringreplace each ('NAME__' <@;"1 0 n) stringreplace&.> <5!:5 {.;:'TIME_TEMPLATE'
   (EXPL_VERB_TEMPLATE  [^:((,:'SMURF__') -: ]) L: 0 ~ verb_bodies ) `: 0

)

nvtxt     =:  (,. '   '&,"1)&.>/ @: ((;:'alg time space') (<1 0)&C.@:('-'&,)@:, each ])
rnd       =:  (',' (8!:1) 0.01 round  (%"1 <./)@:>@:({:"1))
nvrnd     =:  nvtxt@:(, rnd)

time_rpt  =:  dyad define
   9!:7' ' #~# BOXDRAW =. 9!:6''
   9!:17 (1 1) [ BOXPOSN =. 0 0[9!:16''
	
   Q =. (<<<2) { ":  (;:'set alg time space') , (,~ $&'-'@:(1 , {:)@:$&.>@:{.)>,.&.>/ <"_1 each ,1 ]`]`(rjust&.>)`(rjust&.>);.1 <@:>"1 |:  x ,. (<<@:>b1) (, rnd) &> y
	
   9!:7 BOXDRAW
   9!:17 BOXPOSN

   Q
)


   'a1 b1'=:4!:5&.> i.2  NB.  Local and global assigment can't happen for the same name under 'load'

   recf    =. 1 : 0
:
	   s =. y
	   r =. ,:y
	   for_i.
	     i. # x
	    do. s =. (i{x) u s
	        r =. r,s
	   end.
	   r
)

   jackson0=:  + recf f.

   seq     =.  ((&.>) /) (@:((|.@:[) , (<@:]))) (>@:)  NB.  Due to Quintana
   bron0   =:  (+ seq f.~ <"_1)~  NB.  No intermediate results

   seq     =.  <"_1@|.@[ , <@]
   update  =.  (] ,(+&.> {:))/
   phil0   =:  [: > update@seq f.  NB.  Doesn't meet spec

   reboss0 =:  [: > [: +&.>/\.&.|. <@],<"_1@[

   ug      =.  2 : '[: >@:({."1) (2&(u&.>/@:{. , }.)^:(1 < #)^:n @: (; <"_1@:|.)~)'
   bron1   =:  + ug a: f.

   gu      =.  1 : '(u/~ u/\@:|.@:({.~ >:@:#))~'
   bron2   =:  + gu f.

   enshape =.  ] , ($"_ _1~ $)
   oleg0   =:  +/\@enshape f.

   oleg1   =:  (+"_ 0 (0 , +/\))~

   'a1 b1' =:  4!:5&.> i._2
   b1      =:  (;:'a1 b1') -.~(#~ =&({.;:'_base_'))/ |: (split~ i:&2@:(* +/\.)@:=&'_')&> a1

   small   =:  (+: i. 5  ) 1000 H b1 i. 2 2
   medium  =:  (+: i. 500)   10 H b1 i. 2 2
   large   =:  (+: i. 5000)   1 H b1 i. 2 2

   ]A      =: (time_rpt ".&.>) ;: 'small medium large'

See also the matrix to coordinates thread on the Forum.