Live data from Hacker News

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

news.ycombinator.com

31–40 of 69 posts

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

#31
post #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.

I believe the intention was more Zen -- don't write code that isn't really necessary -- and the "use standard libraries" code was separate. (If you are using a language where the standard libraries are so poorly implemented that you can easily write better versions, you might want to consider using a different language.)

I would also add the cargo cult suggestion of "precompute and cache everything you can". Whatever can be done in an indexing process instead of a search process should be; whatever doesn't need to happen at serving time shouldn't.

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

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

> A common mistake is e.g. using the `in` operator with lists in Python, instead of converting the list to a set first. Is this true even for small lists without many duplicates?

Not really

You may be able to compromise and use an ordered list and a binary search.

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

#33
It's a bit unique to each situation, but a few topics that come to mind immediately:

* Profile & measure to narrow down where time is being spent

* Memoize repeated function calls

* Cache data that doesn't frequently change

* Minify web assets

* Gzip compress web output (a la mod_deflate)

* Load balance

* Shard

* Strategic database indexing

* Asynchronous workers when beneficial

* Use the correct data structures

* Minimize Disk I/O

^ I suppose the last few are architecture-related more so than code.

These topics are mostly specific to web, but, here is a "curated list of Web Performance Optimization"

https://github.com/davidsonfellipe/awesome-wpo

Since nobody has quoted Donald Knuth yet (and this quote sometimes gets bent to fit situations that it shouldn't):

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

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

#35
post #27

Earlier quoted context omitted.

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.

I believe the intention was more Zen -- don't write code that isn't really necessary -- and the "use standard libraries" code was separate. (If you are using a language where the standard libraries are so poorly implemented that you can easily write better versions, you might want to consider using a different language.) I would also add the cargo cult suggestion of "precompute and cache everything you can". Whatever…

> If you are using a language where the standard libraries are so poorly implemented that you can easily write better versions, you might want to consider using a different language.

Standard libraries are typically generalized for a wide array of use cases, you can easily write much faster implementation specialized to your needs if you have any idea what you are doing.

Sometimes standard libraries handle such ridiculous cases (e.g. many ES5 array methods and Function.prototype.bind) that your needs don't even need to be that special.

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

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

Post reply on HN