Instead of rewriting critical sections of your code, consider running the code on a machine with a faster processor, more cores, more RAM memory, faster bus speeds, and/or reduced disk latency. This suggestion may seem like a glib cop-out, but it's not. Sure, using a bigger machine for your analytics sometimes means extra money, but your time, dear reader, is money too. If, over the course of your work, it takes you many hours to optimize your code adequately, buying or renting a better machine may actually prove to be the more cost-effective solution.
Going down this road needn't require that you purchase a high-powered machine outrightly; there are now virtual servers that you can rent online for finite periods of time at reasonable prices. Some of these virtual servers can be configured to have 2 terabytes of RAM and 40 virtual processors...