Live data from Hacker News

Ask HN: What are the 20% tips that will get my code to performance 80% faster?

news.ycombinator.com

51–60 of 69 posts

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#51
Have a test suite that is 95% unit tests. And structure it in a way that tests can run in parallel, across all cores.

FROM THE BEGINNING. Because it's nearly impossible to make this change after the fact.

Also, use a code profiler and commit the profile data to the repo! This will allow you to plot performance issues over time and watch for big upticks.

This alone will make your code orders of magnitude times better (and likely, faster). You will catch concurrency issues before they become a huge problem, your test suite runtime won't be as much as an impediment to your development process, and you will simply write better, more modular, more maintainable code by having to unit-test every component in isolation.

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#52
Measure and profile the code will always be the best thing to do but for simple 20% effort tips I think it would be:

* Don't write your own data structure, stick to the standard library or, in some rare cases, popular and well-known 3-rd party lib.

* Always prefer a Hash or a Set over anything else.

* Beware of any nested loops.

* Have some local / external caches and try memoization.

* Try parallelizing or better, non-blocking wait for all I/O operations.

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#54
post #41
post #20

Stop worrying about how well your code performs and focus on more important things. Seriously. I'm not saying that code performance isn't important; of course it is. It's just that if you're already fairly competent, then code performance is probably not your biggest problem. You're looking for a magic bullet that may not be there. I'd be more concerned with: - How easily can another programmer maintain your code? We…

> Stop worrying about how well your code performs and focus on more important things. It's very naive to think that that's an acceptable tradeoff in every situation. Would you accept it if chrome/FF/ suddenly consumed an extra 30% of memory and CPU time after a single update, because the developers decided that the HTML rendering code wasn't readable enough? Or if you're writing code for cars that needs a response ti…

yep there are also a lot of situations in enterprise where you don't exactly know how your code will be used in near future (in Because of that I'd say.... it's not like I see a lot of people get raked over the coals for it, but there is definitely some organizational disappointment when a silly scaling bug crops up in production[1]. I think as a mature dev it is proper to internalize as many performance-related techniques as possible so that you can write more performant code with each subsequent project. Sometimes it doesn't even affect the amount of initial coding hours, it is just one of the freebie gains that comes with experience.

[1] Many more such bugs would probably occur if the company didn't occasionally get around to load-testing, so that should be part of CI or dev cycle if possible.

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#55

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…

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.

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#56

I wish I knew whose quote this was: "Use your intuition to ASK questions, not to answer them." Specific to this topic: Use a profiler to determine where you're spending your time. Don't guess; measure. When you find a candidate area to work on, determine whether caching or a fundamental algorithm change could work. Those will usually get you far greater gains than optimizing code as-is.

Would truly help to know what kind of code echo272 wants to run faster. Language and environment.

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#57
post #56

I wish I knew whose quote this was: "Use your intuition to ASK questions, not to answer them." Specific to this topic: Use a profiler to determine where you're spending your time. Don't guess; measure. When you find a candidate area to work on, determine whether caching or a fundamental algorithm change could work. Those will usually get you far greater gains than optimizing code as-is.

Would truly help to know what kind of code echo272 wants to run faster. Language and environment.

Thank you vardump. It's numerical linear algebra kernels on C, in a multi-core environment.

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#58
post #26
post #5

Use the correct algorithms. O(log n) beats O(n) beats O(n log n) beats O(n^2) almost every time. A common mistake is e.g. using the `in` operator with lists in Python, instead of converting the list to a set first.

there's a constant factor hidden in all of those, and it's not free. a tree can often be slower than a simple array for lookups, even if the tree is O(log n) and the array is O(n).

Especially since the array will probably be linear in memory, so will work well with the cache, and the tree probably will have jumps all over the RAM unless the implementation uses a backing array.

Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?

#59
post #20

Stop worrying about how well your code performs and focus on more important things. Seriously. I'm not saying that code performance isn't important; of course it is. It's just that if you're already fairly competent, then code performance is probably not your biggest problem. You're looking for a magic bullet that may not be there. I'd be more concerned with: - How easily can another programmer maintain your code? We…

I like this list much better than much of this thread. A lot of programming doesn't NEED to be that efficient, and it's much better to learn the "soft" stuff and how to properly maintain projects
Post reply on HN