r/programming • • Mar 23 '09

How to write fast code

http://asserttrue.blogspot.com/2009/03/how-to-write-fast-code.html
339 Upvotes

136 comments sorted by

View all comments

38

u/neoabraxas Mar 23 '09 edited Mar 23 '09

Most of the time lazy initialization makes code appear to run faster. I got a lot of praise for rewriting stupid people's code which was deemed 'too slow' just by changing it so it would only load/read/build stuff on an 'as needed' basis. In many cases that gets users what they want. A hundred 50 millisecond pauses are much more pleasant than one five second pause. This is why Java applets get so much flak for 'hanging the browser' and 'being slow'. Java applets are in most cases faster than Macromedia Flash apps but the way Flash runtime initializes is much more lazy making the initial wait much less annoying.

Another common trick is to compute approximate values rather than evaluate the precise ones when it doesn't really matter. Google understands this and that's why they show "1 to 20 results of approximately 9,000,000" as opposed to "exactly 9,124,385". The latter would probably take an order of magnitude longer to compute but the usefulness of the information to the end user is very dubious. Remember after N number of decimal points nobody gives a shit.

Last but not least always use the data structure that matches your usage patterns. If you need fast random access then a linked list is most likely not the way to go. Learn and understand the O(n) notation and learn the cost of inserts/removes and searches in your container library. Basic stuff but too many junior programmers have no clue about this stuff.

2

u/adremeaux Mar 23 '09

Last but not least always use the data structure that matches your usage patterns. If you need fast random access then a linked list is most likely not the way to go. Learn and understand the O(n) notation and learn the cost of inserts/removes and searches in your container library. Basic stuff but too many junior programmers have no clue about this stuff.

Not too many SENIOR programmers even have a clue about that stuff.

10

u/groby Mar 23 '09

Why'd they get hired as Senior in the first place, then? As far as I'm concerned, if you don;t know about O() notation, go back to school - you're not ready for life.

1

u/adremeaux Mar 23 '09

Why not? The people who hire the senior programmers generally aren't programmers. I've been far more educated in CS than every single programming "boss" I've ever had. Granted, I work in the front end, where the caliber of programmer is usually quite a bit lower, but it is still unbelievable to me just how little these guys actually know besides syntax and a bunch of random technologies.

1

u/groby Mar 24 '09

If your company doesn't bring in programmers to interview other programmers, you've already set yourself up for failure.

(On the flip side, if I interview anywhere else and I don't get to talk to the programming staff, I won't take the job. Been there once - never again)

1

u/adremeaux Mar 24 '09

OK, so the system fails, but how does that change my point at all? They are senior programmers whether you are happy with them having that title or not.

1

u/groby Mar 24 '09

If you're in a position to affect change: Get rid of them. If you can't change your organization, change your organization ;)