Compiled function slowdown
- To: mathgroup at smc.vnet.net
- Subject: [mg86682] Compiled function slowdown
- From: mm1q <epv001 at lvc.edu>
- Date: Tue, 18 Mar 2008 04:50:04 -0500 (EST)
Hello, In a program I'm currently working on, I need to loop through a function ~100,000 times. I thought I'd try compiling it to save on time. However, it takes more than 10 times longer to run than the uncompiled (module) version. Are there any general guidelines as to when a compiled function will be slower than its uncompiled counterpart in Mathematica (such as with the use of many conditional statements)? I've included my function below (sorry if it's a mess; e1, e2 and fComp are very simple compiled functions), though a general response to the above question would be just fine. Thanks! generator = Compile[{stepTotAccum_Real, {dxTable, _Real, 1}, pTile_Real, area_Real}, Do[ While[True, u = Random[]; If[ u < stepTotAccum, stats = 1 + 4*Floor[u/pTile]; cand = (u - dxTable[[stats + 2]])/dxTable[[stats + 1]] + dxTable[[stats]]; If[Random[]*dxTable[[stats + 3]] < fComp[cand], Return[cand * (-1)^Random[Integer]] ]; , cand = e1[u]; If[e2[Random[], cand, area] < fComp[cand], Return[cand * (-1)^Random[Integer]] ]; ]; ]; ] ];
- Follow-Ups:
- Re: Compiled function slowdown
- From: Daniel Lichtblau <danl@wolfram.com>
- Re: Compiled function slowdown