Tuesday, July 26, 2011

Chasing unicorns

Speeding up R proved to be almost hypnotic in the focus it drew from me. Hours went by instantly, it seems. I spent more time than I wanted, but I'm done now, and it runs pretty well. I wasted the last few hours, though, except for deeper knowledge of R.

My R graphs now use parallelization. I had trouble for a while because I had forgotten to close a graphics object for the line graph. It wasn't a problem without threading, as long as I didn't have too many objects open, but it broke parallelization completely. I found that problem and tightened up the R object code. It's still rough, and I haven't fully explained the parallelization additions in comments, but it works well. I also made another change in caching. Earlier, I made it so if a graph that looks the same already exists, it will load that instead of recreating it. However, there wasn't any such limit on a single session. One command to R could have code to recreate the same graph over and over. I fixed that, and now instead of making 80 for a new teacher page, it makes around 20. This jump will be a lot less drastic when groups are larger and we have more variation, but it helps. This really helped me find and fix the problem with threading, as there was less R code to look through.

Calling on R to make all of the graphs for a teacher page took about .5 seconds before threading. After, it took around .25. The rest of the program takes less than .05. The page loads in about 1.5-2.5 seconds, with no cache of graphs. I hadn't consciously expected a figure, but .25 still seemed high, so I made an R "Hello World" program and profiled that. It took about .23 seconds, so most of that had to be going into starting R. (You execute shell code in PHP using the exec function. exec doesn't return until whatever you executed stops running [generally]. That's how I could use a PHP profiler to find R's execution time.) I tried a couple things to speed that up, and that's when I saw the unicorn.

It was pretty stupid. I tried running the HW program in BATCH mode. I'd previously discounted it for a number of reasons, but this time it ran in .015 seconds. Ooh, BATCH mode exists with all of its problems because it loads R faster!, I thought, and spent the next few hours chasing that idea. Nope. It ran fast because exec returned early. exec returned early because it ran into a permissions error. The speedy unicorn turned out to be a mirage.

No comments:

Post a Comment