Hacker News new | ask | show | jobs
by chrisbennet 4149 days ago
Trust no one, not even yourself: Profile your code.

That said, if you aren't profiling already you are probably trolling us to get suggestion for your next blog article. ;-)

As others have mentioned, use better algorithms i.e. it makes no sense to speed up a bubble sort routine instead of using a quick sort approach.

A lot of speed improvements come from just not doing stuff in the first place vs. speeding up a naive or brute force approach.

For example, suppose you want to determine when something enters the frame of a camera image that is 640x480 pixels big.

The naive approach would be to compare every pixel of the frame to every pixel of the previous frame. The area of the frame is Width x Height: 307,200 pixels.

A more efficient approach is to process just the edges (perimeter) of the image/frame since nothing can enter the frame without crossing the perimeter first. The perimeter is 2 x Width + 2 x Height: 1080 pixels.

The 1st approach is 284 times slower (307,200/1080) and no amount of optimizing it is going to make it as fast as the 2nd approach.

1 comments

If your pixels are a byte each and the data is 64-byte aligned in memory, you're still reading 640 * 2 + 128 * (480 - 2) = 62464 bytes from memory, not 1080. That's because you can't read less than a cache line, which happens to be almost always 64 bytes. Performance win could be a lot less than what you expected.