More about binary search in Erlang
Ruslan Spivak - - August 17, 2007In my previous post My Erlang binary search I’ve implemented binary search in Python and Erlang as small exercise for myself after reading Half-baked Ideas blog post. Good article pointing to specifics of implementation of that algorithm in Erlang was posted on Erlane’s blog which is in a nutshell: binary search in Erlang implemented on list data structure is less efficient than linear search.
So to grasp why linear search which is O(N) is faster in Erlang then binary search which is in theory O(logN) go and read that article.
In Python list data structure (I’m talking here about CPython) is implemented as mutable and extensible vector of references to objects(we might say array of pointers), here is the excerpt from listobject.h:
typedef struct { PyObject_VAR_HEAD /* Vector of pointers to list elements. list[0] is ob_item[0], etc. */ PyObject **ob_item; /* ob_item contains space for 'allocated' elements. The number * currently in use is ob_size. * Invariants: * 0 <= ob_size <= allocated * len(list) == ob_size * ob_item == NULL implies ob_size == allocated == 0 * list.sort() temporarily sets allocated to -1 to detect mutations. * * Items must normally not be NULL, except during construction when * the list is not yet visible outside the function that builds it. */ Py_ssize_t allocated; } PyListObject;
So Python version works as expected regarding efficiency. While my goal of implementing binary search algorithm in Erlang was learning Erlang and not particulary developing optimized version of that algorithm or using another one it’s always good to pick new tricks of the trade along the way and for me the issue with my implementation of binary search in Erlang just boils down to: your experience and know your tools (language, os, editor(emacs
, etc )
As Aldous Huxly said: “Experience is not what happens to you; it’s what you do with what happens to you.”. So make errors (not many
in your programs, fix them and gain new experience with your new language.
Another post by Erlane team is good reading describing paradigm shifts when you are newbie and are on your own way to Erlang/OTP wizardry.
Categories: Blogs Ruslan Spivak
Erlang on Twitter
» true_droid (Alex): RT @marick: This book on Elixir (a language on top of Erlang) looks interesting: http://t.co/3qNXsON6Vt Dave Thomas has an eye for up-and-c…
» diaffalo (diaffalo): RT @marick: This book on Elixir (a language on top of Erlang) looks interesting: http://t.co/3qNXsON6Vt Dave Thomas has an eye for up-and-c…
» erlangtp (Erlang): @Addictator_22 tadi erlang ngatain om fadhil SB ya?? sabar om fadhil itu fakta :D
» ericmoritz (Eric Moritz): RT @heinz_gies: I must say writing protocol implementations in #Erlang is quite fun!
» brendagracee (Brenda Grace): @Etha_Yummy @Muh_Erlang mn jgn idk potoke
» tita_titutt (Shinta N. Mayliyanti): Huuu, insomnia tauk :D “@Erlang_07: @tita_titutt gk bisa yyaa? kasian XD”
» IiansArdians (iian sparkled™ ): @gojekGentho Kenopo yo sebagian cah wedok nek foto karo nyekeli dodo? *spaneng
@Erlang_Bolank7 @harri_tm @monztersuck @monomalvano
» clofresh (Carlo Cabanilla): Fascinating description of how Erlang scheduling works http://t.co/bc20GVnApr
» RamCSingh (Ram C Singh): RT @totallyerlang: The two day Erlang Camp Amsterdam in August is being sponsored by Spil Games. http://t.co/aEHM8xm9u3
» Muh_Erlang (M. Erlangga Pangestu): @Etha_Yummy @brendagracee yakin kalian jam 4?? Bleknyo pcak mlem nian kamu. Plaju glak macet lwat jam segitu
Statistics
Number of aggregated posts: 10650
Most recent article: May 20, 2013
Latest comments
» Moraru on This is Why You Spent All that Time Learning to Program: It is true that computer science was a pain in the back at time that i’ve had to learn it…
» Commercial hand dryers on Couchbase Meetup at new HQ: Buy online from here where you will get so much of variety in Commercial hand dryers for people. If you…
» Fort McMurray Homes on Motivated Reasoning and Erlang vs Python vs Node: I don’t really understand why this post is motivational? I don’t even see a post, just a title. Fort McMurray…