Live data from Hacker News

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

news.ycombinator.com

21–30 of 69 posts

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

#22
Code that you don't write consumes zero clock cycles.

Learn your language well, so that you can use data structures and builtin functions as they are intended, and you can expect them to be pre-optimized for you. Learn in principle what is optimal, and write in that fashion.

Know the documentation, and follow best practices that the docs recommend - when the compiler/language gets updated, you'll get free optimizations.

Use semantic names, none of this i, j, x, y stuff (unless you're writing totally abstract math) and don't use nebulous names like "helper," when you can be more precise. Better names help you think more precisely about your code and what you want it to do, so you can avoid unnecessary transformations, materializations, and calculations.

When you've finished the program (all the unittests and acceptance tests pass), then profile. Focus on the bottlenecks. In this way, you'll get your optimal Pareto performing code.

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

#23
I/O latency is usually a major "hidden" cost on programs that deal with large sets of data, not only when writing the solution to disk or loading the data, but memory access in particular. Bottlenecks on the memory hierarchy is something to pay a close attention to.

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

#24

I suggest you to read a book about algorithms. This one is pretty good: "Introduction to Algorithms" by Cormen and Rivest.

I second that, but would add data-structures to it. They go hand in hand. Being familiar with whats available will let you choose the correct one.

Using the right data structure and algorithm for the job can your code significantly faster.

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

#25

Earlier quoted context omitted.

>always put the quickest executing condition first. isn't it put the most probably true condition first?

You probably mean to put the probably false condition first, since if the first condition is false, the others are not tested anymore.

If the first condition is the slowest you can't get faster than that. But if the first is faster it might be the only one executed.

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

#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).

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

#27

Code that you don't write consumes zero clock cycles. Learn your language well, so that you can use data structures and builtin functions as they are intended, and you can expect them to be pre-optimized for you. Learn in principle what is optimal, and write in that fashion. Know the documentation, and follow best practices that the docs recommend - when the compiler/language gets updated, you'll get free optimizatio…

Code that you didn't write but that is being executed still consumes clock cycles. Standard library functions and data structures still have code that executes on the machine, you can't just handwave it away as consuming zero cycles just because you didn't write it.

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

#28
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…

Ed is right in that all these things matter as much or more than making performant code but at the same time your question suggests that performance is your main issue at the moment. If that's not the case then please take Ed's advice to heart and invest your time in all of the above, only concentrate on performance if you are 100% convinced that is what will give you the best return on time invested.

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

#30
Respect CPU caches and locality. Keeping your data structures in cache or even packed together can result in a nice performance gain.

I've seen benchmarks where inserting into an array was significantly faster than inserting into a linked list. This can be surprising considering most algorithm texts I've seen say the complexity of the latter is better. From what I understand locality is part of the reason inserting into the array is faster.

Post reply on HN