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.
Ask HN: What are the 20% tips that will get my code to performance 80% faster?
21–30 of 69 posts
Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?
#22Learn 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?
#23Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?
#24I suggest you to read a book about algorithms. This one is pretty good: "Introduction to Algorithms" by Cormen and Rivest.
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?
#25Earlier 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.
Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?
#26Use 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.
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?
#27Code 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…
Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?
#28Stop 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…
Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?
#29Re: Ask HN: What are the 20% tips that will get my code to performance 80% faster?
#30I'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.