Impressed by Slow Code
Programming in the 21st Century - James Hague - April 30, 2011At one time I was interested in—even enthralled by—low-level optimization.
Beautiful and clever tricks abound. Got a function call followed by a return statement? Replace the pair with a single jump instruction. Once you’ve realized that “load effective address” operations are actually doing math, then they can subsume short sequences of adds and shifts. On processors with fast “count leading zero bits” instructions, entire loops can be replaced with a couple of lines of linear code.
I spent a long time doing that before I realized it was a mechanical process.
I don’t necessarily mean mechanical in the “a good compiler can do the same thing” sense, but that it’s a raw engineering problem to take a function and make it faster. Take a simple routine that potentially loops through a lot of data, like a case insensitive string comparison. The first step is to get as many instructions out of the loop as possible. See if what remains can be rephrased using fewer or more efficient instructions. Can any of the calculations be replaced with a small table? Is there a way to process multiple elements at the same time using vector instructions?
The truth is that there’s no magic in taking a well-understood, working function, analyzing it, and rewriting it in a way that involves doing slightly or even dramatically less work at run-time. If I ended up with a routine that was a bottleneck, I know I could take the time to make it faster. Or someone else could. Or if it was small enough I could post it to an assembly language programming forum and come back in a couple of days when the dust settled.
What’s much more interesting is speeding up something complex, a program where all the time isn’t going into a couple of obvious hotspots.
All of a sudden, that view through the low-level magnifying glass is misleading. Yes, that’s clearly an N-squared algorithm right there, but it may not matter at all. (It might only get called with with low values of N, for example.) This loop here contains many extraneous instructions, but that’s hardly a big picture view. None of this helps with understanding the overall data flow, how much computation is really being done, and where the potential for simplification lies.
Working at that level, it makes sense to use a language that keeps you from thinking about exactly how your code maps to the underlying hardware. It can take a bit of faith to set aside deeply ingrained instincts about performance and concerns with low-level benchmarks, but I’ve seen Python programs that ended up faster than C. I’ve seen complex programs running under the Erlang virtual machine that are done executing before my finger is off the return key.
And that’s what’s impressive: code that is so easy to label as slow upon first glance, code containing functions that can—in isolation—be definitively proven to be dozens or hundreds of times slower than what’s possible on a given CPU, and yet the overall program is decidedly one of high performance.
(If you liked this, you might enjoy Timidity Does Not Convince.)
Categories: Blogs Programming in the 21st Century
Comments
“Premature optimization is the root of all evil”.
Posted by Jens Rantil on 01 May 2011 at 12:03@Jens Rantil: Not all optimization is premature.
Posted by M Campbell on 02 May 2011 at 04:59The rut how you conscientious to talk about some posts at here seems to me different. There are unquestionably other posts at here,but i didn’t bargain measure post related to projects like pass4sure. if someone have information about it,do tell me! Well any updates related to this post. if yes than do tell me!actually i came here while surfing net to get data related to projects of pass4sure HP2-Z17 and find this post different one. Is there anyone having information about pass4sure PMI-002. if yes than do tell me! By the way what is your opinion about insurance AND interest. is it ethical, i don’t think so!
Posted by Chung33 on 16 Jul 2011 at 11:43I also spent a long time doing that before I realized it was a mechanical process. furnace company greenwood indiana
Posted by gema on 07 Sep 2011 at 23:49I have been reading blogs on this subject for a few hours now and your content is the most informed I have read thus far. Thanks for putting this information in one place.essay services reviews
Posted by top essay writing services on 04 Jan 2012 at 16:46Post is nicely written and it contains many good things for me. I am glad to find your impressive way of writing the post. Now it become easy for me to understand and implement the concept. Thanks for sharing the post”ラルフ ポロシャツ”
Posted by vindiesel on 06 Jan 2012 at 17:03Thanks for sharing this information. I really like your way of expressing the opinions and sharing the information. It is good to move as chance bring new things in life, paves the way for advancement,
final expense
I completely agree with you. I really like this article. It contains a lot of useful information. I can set up my new idea from this pos”oakley eyewear”
t.
Great information, thanks so much for sharing it.
jobs for women
I would be very thankful if you continue with quality what you are serving right now with your blog…I really enjoyed it…and i really appreciate to you for this….its always pleasure to read so….Thanks for sharing!!!
“Essay writing UK”
In communications and information processing, encoding is the process by which information from a source is converted into symbols to be communicated. Decoding is the reverse process, converting these code symbols back into information understandable by a receiver. Thanks a lot.
Regards,
coursework help
The semiochemicals trigger the fungal organism to react in a specific manner, while if the same chemical molecules are not part of biotic messages, they do not trigger the fungal organism to react. This implies that fungal organisms can differ between molecules taking part in biotic messages and similar molecules being irrelevant in the situation. Thanks.
Regards,
cordless chainsaw reviews
It is really nice article having nice information. I would like to appreciate writer ‘s nice effort. Keep this sort of posting in future as well. I will be back after some time. nutrition food pyramid
Posted by shine on 08 Feb 2012 at 22:34Most of the thousands of human languages use patterns of sound or gesture for symbols which enable communication with others around them. Languages seem to share certain properties although many of these include exceptions. Thanks.
Regards,
toronto lofts
Nonverbal communication describes the process of conveying meaning in the form of non word messages. Research shows that the majority of our communication is non verbal, also known as body language. Thanks.
Regards,
traveljobs.com/translator
This was really very nice posting in this blog. I had really liked the stuff very much.
Logo Design Contest Online
Toronto LOFTS are unique properties that fit the need of unique individuals. On Mr. Loft’s website you can find the most recent listings
Posted by Toronto Lofts on 12 Mar 2012 at 11:03Toronto LOFTS are unique properties that fit the need of unique individuals. On Mr. Loft’s website you can find the most recent listings
<a >Toronto Lofts</a>
Toronto LOFTS are unique properties that fit the need of unique individuals. On Mr. Loft’s website you can find the most recent listings
Toronto Lofts
Add comment
Erlang on Twitter
» unnali (Len): dear erlang where are my monads yours cheerfully l
» ajfeed (Ajinkya Feed): Erlang: Erlang : UDP socket usage example with gen_udp: submitted by dzysyak [link] [comment] http://t.co/WMpJtySv
» rianindahinyonk (Rian Indah Syafitri): RT @fathiaamandaaa: RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur semoga makin banyak proyek nya ya.
» ericmoritz (Eric Moritz): RT @Burbass: I can now control my Mindstorm Lego car with Erlang. #erlang #mindstorm http://t.co/Jn78yViH
» dalnefre (Dale Schumacher): RT @Burbass: I can now control my Mindstorm Lego car with Erlang. #erlang #mindstorm http://t.co/Jn78yViH
» Erlang_ABNIC (Erlangga .A): “Dream, Believe, and Make it Happen”. ☺ RT @cjerikho829: “Believe”
» aidilnasution (M Aidil Nasution): RT @fathiaamandaaa: RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur semoga makin banyak proyek nya ya.
» indytertuing (indy hamid): ƪ(^ヮ^)ʃ RT @fathiaamandaaa: RT @indrasan: selamat ulang tahun saudara reza erlang @rezasur semoga makin banyak proyek nya ya.
» rvirding (Robert Virding): RT @Burbass: I can now control my Mindstorm Lego car with Erlang. #erlang #mindstorm http://t.co/Jn78yViH
» pikuseru (Dave Birdsall): @mho105 I bought an O’Reilly book about Clojure, a language for the JVM. Bought Scala and Erlang books over a year ago. Just interesting.
Statistics
Number of aggregated posts: 10503
Number of comments: 2135
Most recent article: May 21, 2012
Latest comments
» DRS786 on 25 May 2012: Poznan Erlang User Group Event: I’m going!
» the tantric way in london on TextOne HD for webOS: Interesting articles are published here. By reading it I acquired great deal of knowledge on various subject. Thank you for…
» israeli jewelry on 08 February 2012: Erlang Express 3-day Course in San Francisco on 8 February: It is a wonderful blog. It helps me out a lot. Thank you. I really need help in development and…