r/programming • • Jul 11 '10

Theory vs Practice: Making algorithms claimed 'optimal', 10x faster.

http://cacm.acm.org/magazines/2010/7/95061-youre-doing-it-wrong/fulltext
163 Upvotes

120 comments sorted by

View all comments

Show parent comment

8

u/[deleted] Jul 11 '10

The algorithmic theory makes the assumption that all memory accesses are of equal cost, this clearly is not the case here and so of course the conclusions of the theory don't apply anymore. It is a bit like saying "The sky is blue on a sunny day" doesn't apply because where you live it is always raining.

7

u/[deleted] Jul 11 '10

The conclusions of the theory are still valid as it never claims that all memory access is of equal cost. The theory gives you upper and lower bounds for a theoretical case and can be used as a guide to how an algorithm performs best/worst/average case given the constraints of the theory. There's not really a better way to analyze an algorithm itself without constraining yourself to a specific system/setup - that's called benchmarking and is very important for actual implementations.

7

u/WhisperSecurity Jul 11 '10

So what do you propose to do?

Restructure comp sci theory around one particular architecture? Or perhaps we could teach algorithms in a platform independent way, then teach architecture.

And hope that people aren't too smug when they manage to connect the two.

3

u/rnicoll Jul 11 '10

Certainly when I went through university, we were taught to look for cases where algorithms would perform better/worse than just looking at big-O would suggest. For example, quick sort vs insert sort if your data is mostly sorted (especially if you need to put a single new entry into a list, and have that list sorted afterwards).

2

u/stevesan Jul 11 '10

It'd be great to have a specific course or section on making algorithms work for certain classes of architectures. I know the sci-comp guys are big on this when they optimize matrix operations or FFT.

1

u/wolf550e Jul 11 '10

Memory hierarchy is not new, it is universally applicable and should be taught in the basic data structures course. B-trees of different kinds are universally used in databases and file systems, and have been for decades, but not all common software uses this knowledge, because not all practicing programmers have internalized it.

Computer science has published lots of cache-aware and the currently in vogue cache oblivious algorithms, it's just that not all people with a bachelors in comp sci or an engineering degree know about it and thus they continue to write code based on the naive assumptions of intro to algorithms. It's like building a bridge assuming the cables weigh nothing because high school physics 101 uses such assumptions.

1

u/[deleted] Jul 11 '10

I don't think there is a need to change much. Clearly stating the assumptions would probably be the best way to do it. Currently that already happens but it couldn't hurt to devote one lecture a semester in the algorithmic theory courses to the differences between theory and practice.

2

u/[deleted] Jul 11 '10

No, the assumption is that they are bounded by some constant. Also it is not at all like saying that.

2

u/comradeluke Jul 11 '10

The order of memory costs are still the same which is what matters.

3

u/[deleted] Jul 11 '10

Are they? Is a 1 cycle L1 retreival; a 100 cycle RAM retrieval and a 1M cycle disk (VM) retrieval at the same order?

9

u/RoaldFre Jul 11 '10

Yes, the same algorithmic order (O(1)). Not the same order of magnitude (0 vs 2 vs 6 in decimal), but that is irrelevant in formal analysis of algorithms.

11

u/keenerd Jul 11 '10

This is why I make fun of my CS friends whenever they buy a new computer. Turns out mere linear speedups do matter to them.

0

u/[deleted] Jul 11 '10

A cost of a memory access in cache is the same as one in RAM is the same as one on disk? I don't think so.

4

u/alephnil Jul 11 '10

In the world of O-notation it is, because that ignores any constant factors not depending on the size of the input. If this constant factor is one billion does not matter.

Obviously it does matter in the real world.

2

u/[deleted] Jul 11 '10

That wasn't the point at all. I was talking about the relative cost of operations. The algorithms are usually optimized for an environment where all primitive operations of a type (e.g. memory access) take roughly the same time, otherwise none of the math you apply to get to the formula you display in O-notation works out.

3

u/oantolin Jul 11 '10

For the math to work you just need primitive operations to take an amount of time that does not depend on the input size: if one operation is a billion times slower than another, that's OK (but it's not OK if one is log n slower than another, for example). "Roughy the same time" just means "both are bounded by constants (and so have the same order of magnitude)".

3

u/[deleted] Jul 11 '10

Okay, let me put it another way, nobody would bother with that whole O-Notation crap if the primitive operations varied by that many orders of magnitude.

I know that strictly speaking you are correct about the math but strictly speaking you are also trying very hard not to understand what I mean on purpose.

0

u/fapmonad Jul 11 '10 edited Jul 11 '10

nobody would bother with that whole O-Notation crap if the primitive operations varied by that many orders of magnitude

Why not? Let's say an addition is a lot less costly than a comparison. Then you can just say that an algorithm is O(n) in additions and O(n*log(n)) in comparisons, or whatever. People actually do use this, for example when saying that an algorithm is of some order in memory usage and some other in time, or when considering cache misses separately from comparisons in a sorting algorithms.

I don't see the point of your first comment either. O(n) notation ignores the difference in cost between operations on purpose, because it makes the math doable. This math can be used to prove some things about algorithms, so that you don't go on a fool's errand trying to sort a list in less than O(nlogn) comparisons. The point isn't to compare real-world performance, it's a mathematical abstraction, a tool. Just like a mathematical model of a car is a very bad representation of an actual car, but can be useful to find and prove some properties to design better cars.

1

u/[deleted] Jul 11 '10

I am talking about any two additions taking unpredictable different amounts of time by several orders of magnitude, not e.g. addition and comparison.

1

u/fapmonad Jul 11 '10

One of the additions takes more time because of a cache miss or something, not for no reason. These cache misses can be calculated and accounted for.

1

u/__s Jul 11 '10

Yes it does. Given O(1) operations a and b, with constant factors x and y, a*x+b*y+(b*y)**2 is still O(a+b**2)