From WikiChip
Editing schlemiel the painter's algorithm

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.

Latest revision Your text
Line 1: Line 1:
{{title|Schlemiel the Painter's Algorithm}}
 
 
'''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 [[Wikipedia:Joel Spolsky|Joel Spolsky]] in late 2001.
 
'''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 [[Wikipedia:Joel Spolsky|Joel Spolsky]] in late 2001.
  
Line 29: Line 28:
 
</source>
 
</source>
  
And so forth, which looks very clean and produces <code>"one, two, three, four"</code>. Unfortunately, for every {{C|string.h/strcat|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 <math>O(N)</math> is implemented above as <math>O(N^2)</math>.  
+
And so forth, which looks very clean and produces <code>"one, two, three, four"</code>. Unfortunately, for every {{C|string.h/strcat|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)\).  
  
 
=== Generic example ===
 
=== Generic example ===
Line 51: Line 50:
 
</pre>
 
</pre>
  
If the function was to be used on a simple file with 2 lines, function ''file_get_line'' will end up reading line 1 to get the first line and then it will read line one again and line two to get to the second line, resulting in 3 line scans. If the file was to have 10 lines, ''file_get_line'' will end up reading a total of 55 lines (<math>\sum\limits_{i=1}^{10} i</math>) over and over. It can be seen that the number of operations done grows very quickly even for moderately small files. A file with just 10,000 lines which could usually be read in just 10,000 seeks will result in <math>\sum\limits_{i=1}^{10000} i = {50,005,000}</math> seeks with this implementation.
+
If the function was to be used on a simple file with 2 lines, function ''file_get_line'' will end up reading line 1 to get the first line and then it will read line one again and line two to get to the second line, resulting in 3 line scans. If the file was to have 10 lines, ''file_get_line'' will end up reading a total of 55 lines (\(\sum\limits_{i=1}^{10} i\)) over and over. It can be seen that the number of operations done grows very quickly even for moderately small files. A file with just 10,000 lines which could usually be read in just 10,000 seeks will result in \(\sum\limits_{i=1}^{10000} i = {50,005,000}\) seeks with this implementation.
 
 
== External links ==
 
* [http://www.joelonsoftware.com/articles/fog0000000319.html Joel Spolsky's Blog Post]
 
  
 
[[category:optimization]]
 
[[category:optimization]]
 
[[category:algorithms]]
 
[[category:algorithms]]

Please note that all contributions to WikiChip may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see WikiChip:Copyrights for details). Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)