[Date Index]
[Thread Index]
[Author Index]
compile speed
*To*: mathgroup at smc.vnet.net
*Subject*: [mg74421] compile speed
*From*: "Boson" <sandro.romani at gmail.com>
*Date*: Wed, 21 Mar 2007 02:43:04 -0500 (EST)
dear mathematica users,
i've written a simple function that works on a pair of binary
matrices:
(mathematica 5.2 linux, on a 32 bit platform)
tab[nx_, ny_, frac_] := Table[If[Random[] < frac, 1, 0], {nx}, {ny}]
nx = 25; ny = 50; frac1 = 0.1; frac2 = 0.5;
p1 = 0.4; p2 = 0.2;
tabrect = tab[nx, ny, frac1];
tabsq = tab[ny, ny, frac2];
testnocomp[mat1_, mat2_, n1_, n2_, pp1_, pp2_] := Module[{tmp, sum,
val},
tmp = mat2; Do[sum = mat1[[k,j]] + mat2[[k,i]];
val = Which[sum == 2, If[Random[] < pp1, 1, tmp[[i,j]]], sum ==
1,
If[Random[] < pp2, 0, tmp[[i,j]]], sum == 0, tmp[[i,j]]];
tmp[[i,j]] = val,
{k, n1}, {i, n2}, {j, n2}]; tmp];
Timing[resnc = testnocomp[tabrect, tabsq, nx, ny, p1, p2]; ]
the result of the timing is
{0.7558840000000013*Second, Null}
since i need high values of nx,ny (~5000) and the loop scales as
nx*ny^2,
i tried to implement a compiled version of the previous function:
test := Compile[{{mat1, _Integer, 2}, {mat2, _Integer, 2}, {n1,
_Integer}, {n2, _Integer},
{pp1, _Real}, {pp2, _Real}}, Module[{tmp, sum, val},
tmp = mat2; Do[sum = mat1[[k,j]] + mat2[[k,i]];
val = Which[sum == 2, If[Random[] < pp1, 1, tmp[[i,j]]], sum
== 1,
If[Random[] < pp2, 0, tmp[[i,j]]], sum == 0, tmp[[i,j]]];
tmp[[i,j]] = val,
{k, n1}, {i, n2}, {j, n2}]; tmp], {{Random[_], _Real}}];
Timing[res = test[tabrect, tabsq, nx, ny, p1, p2]; ]
results are a disaster:
{14.814747999999996*Second, Null}
i'm sure this is related to my poor mathematica programming
experience..
could you suggest me a faster version to solve this problem?
regards,
sandro
Prev by Date:
**Re: simple sequence problem**
Next by Date:
**Re: simple sequence problem**
Previous by thread:
**Re: Function[x...] and #& not equivalent**
Next by thread:
**Re: compile speed**
| |