init:{[npreds; nsubs; proba] prob:: proba p:: (npreds _draw 0) < proba myarray:: (4; nsubs) # (4*nsubs) _draw npreds myarray2:: + myarray answer:: nsubs # -2 answer2:: nsubs # -2 canswer:: nsubs # -2 / the result when using the c function idx:: !nsubs cr:: "./test.so"2:("cruntest";3) ncond:: 4 / number of predicates per subscription } / folded version runtest2:{[iterations] do[iterations answer2:: _n {x[& p[y[x]]]}/myarray ] } / unfolded version druntest1:{[iterations] do[iterations i: 0 j: &p[myarray[0]] while[i < ncond j@: &p[myarray[i; j]] i+:1 ] answer41:: j ] } / while loop version druntest2:{[iterations] do[iterations j: &p[myarray[0]] j@: &p[myarray[1; j]] j@: &p[myarray[2; j]] j@: &p[myarray[3; j]] answer42:: j ] } / call the c function crun:{[iterations] do[iterations cr[myarray;p;canswer] ] } run:{ probabilities: 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.2 0.3 0.4 0.5 0.9 0.95 1.0 runprob'probabilities } numiter: 1000 runprob:{[pro] init[5000; 3000; pro] ` 0: ("Probability is: "), ($pro), "\n" ` 0: ("k using a lamda expression \n") . "\\t runtest2[numiter];" / lamda expression ` 0: ("Probability is: "), ($pro), "\n" ` 0: ("k using a do loop \n") . "\\t druntest2[numiter];" / do loop ` 0: ("Probability is: "), ($pro), "\n" ` 0: ("k using an initialized while loop \n") . "\\t druntest1[numiter];" / initialized while loop version ` 0: ("Probability that predicate holds: "), ($pro), "\n" ` 0: ("C code unoptimized. \n") . "\\t crun[numiter];" / c function } run[]