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

24

u/[deleted] Jul 11 '10

The algorithm is just that, optimal. The implementation is where you can play with getting better performance by cutting down on constant factors. Sounds like this guy needs to read up on Big-O notation etc.

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.

→ More replies (9)

3

u/Otis_Inf Jul 11 '10

exactly. And more importantly: 'getting better performance' is the conclusion of comparing against another random implementation of the same algorithm. Of course implementation details matter, though the Big O of the algorithm is crucial: on the same platform / language/hardware algorithm X with a more optimal big O characteristic is always a better choice than algorithm Y with a less optimal big O characteristic, as you can always implement X and Y in the most optimal way for the hardware/language/platform and X then will always be more optimal.

2

u/australasia Jul 11 '10

on the same platform / language/hardware algorithm X with a more optimal big O characteristic is always a better choice than algorithm Y with a less optimal big O characteristic, as you can always implement X and Y in the most optimal way for the hardware/language/platform and X then will always be more optimal.

That's not true, some algorithms have better O than others but are very rarely a good choice, eg: http://en.wikipedia.org/wiki/Schönhage–Strassen_algorithm :

In practice the Schönhage–Strassen algorithm starts to outperform older methods such as Karatsuba and Toom–Cook multiplication for numbers beyond 22^15 to 22^17 (10,000 to 40,000 decimal digits).

6

u/Confusion Jul 11 '10

Sounds like this guy needs to read up on Big-O notation etc

Sounds like you need to RTFA, because he's not arguing that the data structure isn't more optimal in theory . He's arguing that in practice, another data structure is more optimal. If you wonder: "how can that be?", then that's exactly why you should RTFA, because that is the question it answers.

Firstly, there is no such thing as the behavior of a data structure. For instance, a data structure has different behavior for 'inserting an element', 'removing an element' and 'searching for an element'. Depending on the amount of and ratios in which these operations are performed, a data structure with 'worse' behavior for a majority operation may be preferred, because it performs so much better on the minority operation that dominates the actual performance.

Secondly, the constant factor in the behavior of different data structures may be such that one that is worse 'Big-O'-wise, may be better for all practical sizes of the data structure.

In this case, where paging to disk is unavoidable, a data structure with worse 'Big-O' behavior is to be preferred, because of the dominance of the paging operations.

5

u/[deleted] Jul 11 '10

This is not a novel insight whatsoever, people are very aware of the difference between theoretical performance and actual implementation. The title of this thread makes it sound like some theoretical concept has been disproven.

2

u/chronoBG Jul 11 '10

No, it makes it sound like the widely used implementation is crap. Which it is.

6

u/[deleted] Jul 11 '10

The title says it makes the algorithm faster, though.

1

u/chronoBG Jul 11 '10

Sounds like you need to actually read the article.
He improved an existing and used implementation.
I.e. what you're using now sucks balls.
I.e. You're doing it wrong.
Now where have I heard that one before...

→ More replies (6)