{Arrest This Man, He Talks In Maths } spacer

Blog : Archives : Homepage

With your feet in the air, and your head on the ground . . .


{Wednesday, December 15, 2004}


This morning, I had no code for my next planned experiment.

Tonight, I not only have working "demo" code, I've also cleaned up and massively optimized a lot of the underlying code. The optimization work was especially satisfying. Matlab is fruity enough that you kind of have to trick it into being efficient, and while the documentation is great for quickly learning how to make something that will work, it doesn't give you a lot of info on how to make things go fast. So I'm left to explore by trial & error.

Case in point:

I have a function (called repeatedly within a loop) which produces a dot-array image matrix from a set of {x,y} coordinates. Simple. But it was taking TONS of time. Making it a 'uint8' instead of a 'double' matrix helped, but it was still painfully slow. Solution? Instead of (implicitly) declaring the variable for the matrix inside the function (which requires memory allocation), I passed a 'template' matrix - declared once, on the "outside" - into the function. I wouldn't have guessed that this would work, actually, because my initial thinking was that it was costly to pass big variables in and out of functions. Not so; apparently, by using the same name for the argument TO and the output FROM a function, you can sort of create a "tunnel" in and out of the function, allowing it to avoid creating (and allocating memory for) a new variable. Neat, but not intuitive, at least to me. I wonder whether I'm thinking about that the right way, conceptually. I'm not totally sure.

In another function, which creates the coordinate sets, I figured out a way to eliminate two levels of nesting (which Matlab tends to stumble over), and turn an O(N^2) process into an O(N) process. The latter improvement was possible merely because my original algorithm was blindingly idiotic, but fixing it was still really satisfying. This is code that I've been using to create experimental stimuli for about the last 4 years, now. It worked, but it was always annoyingly slow. Now it blazes.

It serves to remind me how silly it is to be obsessed with hardware & CPU clockspeed, when comparing across platforms. If the software you're using is well-designed and optimized, it will run fast. If it's poorly-designed, it will drag.

As an aside, it's pretty sad that Pedro's not gonna' be pitching for the Sox next year. Yeah, he seemed to be getting old and tailing off a little, but he was a heck of a fun character.

posted by Miles 1:29 AM

Ouch... you made my brain hurt.
So... in an HTML analogy, your code now has a "style sheet" instead of you typing in the modifiers for every individual tag you use, every time you use it? (You load the main program once, then switch the parameters without re-performing the vast majority of the task?)

Sorry, I'm not a programmer, I only know the "easy" stuff like HTML and Javascript. My brain already hurts from my whole day, and I won't be surprised if I completely fail to understand what you've accomplished. But congrats either way. :-)
Post a Comment