From WikiChip
schlemiel the painter's algorithm
Revision as of 19:52, 18 January 2015 by Inject (talk | contribs)

Schlemiel the Painter's Algorithm (also spelled Shlemiel) is a term referring to a class of routines that may seem to perform well under small workloads but prove to be highly inefficient as they scale due to needlessly redundant operations that are performed at a lower level. The term was coined by Joel Spolsky in late 2001.

Background

The term is based on the Yiddish joke involving Schlemiel (He שלעמיל) which means an inept clumsy person. Spolsky explained it in his blog as:

Shlemiel gets a job as a street painter, painting the dotted lines down the middle of the road. On the first day he takes a can of paint out to the road and finishes 300 yards of the road. "That's pretty good!" says his boss, "you're a fast worker!" and pays him a kopeck.

The next day Shlemiel only gets 150 yards done. "Well, that's not nearly as good as yesterday, but you're still a fast worker. 150 yards is respectable," and pays him a kopeck.

The next day Shlemiel paints 30 yards of the road. "Only 30!" shouts his boss. "That's unacceptable! On the first day you did ten times that much work! What's going on?"

"I can't help it," says Shlemiel. "Every day I get farther and farther away from the paint can!"

Example

C

Spolsky used the c's strcat() function to illustrate his point. strcat() concatenates a second string onto the first one by traversing the first string by checking each character and locating the null character that terminates the string. The second string is then copied over to the end of the first string, concatenating them. The old, and new, lengths of the string are discarded once the operation is done. For example:

char string[1000];
strcpy(string, "one");
strcat(string, ", two");
strcat(string, ", three");
strcat(string, ", four");
...

And so forth, which looks very clean and produces "one, two, three, four". Unfortunately, for every strcat call, strcat has to start from the beginning and seek the end of the string all over. This operation becomes more and more costly as the string becomes longer, just as Schlemiel the Painter had to walk more and more to get back to his paint can. An operation that should only take \(O(N)\) is implemented above as \(O(N^2)\).