ivoras’ FreeBSD blog

June 8, 2008

The real power of “scripting” / interpreted languages

Filed under: FreeBSD — ivoras @ 6:03 pm

I’m writing a small project in C (will talk about it later) and I really miss the expressiveness that dynamic languages like Python offer. There’s one more thing in addition to elusive “elegance” and similar nontangible properties: the ability to easily use and implement better algorithms. Yes, since Turing it was obvious that the actual programming language in use is more-or-less syntactic sugar, but you wouldn’t exactly like to spend your days programming infinite tapes of symbols, would you? :)

In this (again, emphasis on “small”) project, there were a couple opportunities where I could make use of a fast data-access structure like a hash table (since I need to store and retrieve a lot of data entries) or dynamic memory allocation (since I don’t want to artificially limit the number of these entries) but I just didn’t feel like writing all that code to implement a hash table in C (or use a heavy external library) and deal with memory reallocation and track all those pointers. Yes, I’m lazy. In a more abstract language I could just instantiate a dictionary and say d[i] = something and this would actually be very efficient and take care of memory allocation automagically. Since I limited myself to basic C, I chose simpler algorithms like linked lists and evil static arrays on stack. Ironically, these structures would be comparatively significantly more inefficient in Python.

Of course, at its roots this can be stripped down to be simply a choice between using pre-packaged routines instead of writing your own (aka the NIH problem), but in this case it would actually make my simple program faster and more efficient – despite the overhead of an interpreted, dynamic language.

There are many more similar cases – programmers write bubblesorts in C because they are easy to implement, while going to a higher level of abstraction they could just write mylist.sort() and would get QuickSort or some other efficient algorithm for free, etc. etc.

Does anyone know of a library / collection of algorithms for C similar to glib only BSD-licensed? (Yes, I know about C++ algorithms, I don’t want to use C++).

8 Comments

  1. devel/apr contains some useful stuff like rings, hash tables, and queues if that license is acceptable. OpenSSL has a hash table implementation too, lhash(3), haven’t tried it myself though. Neither really includes as much generally useful stuff as glib.
    I’ve been doing more c++ lately just because STL and Boost are amazing.

    Comment by tony — June 8, 2008 @ 10:09 pm

  2. [...] The real power of

    Pingback by Abstraction » The real power of — June 10, 2008 @ 7:24 am

  3. [...] Comment on The real power of

    Pingback by Abstraction » Comment on The real power of — June 10, 2008 @ 1:54 pm

  4. [...] Ivan Voras: The real power of

    Pingback by Abstraction » Ivan Voras: The real power of — June 10, 2008 @ 6:27 pm

  5. devel/libmowgli is ISC licensed and contains some useful stuff. but it’s mostly for my own code (although suggestions and patches are accepted provided they have a usecase somewhere).

    Comment by William Pitcock — July 9, 2008 @ 9:46 am

  6. Thanks, libmowgli looks interesting!

    Comment by ivoras — July 9, 2008 @ 9:49 am

  7. You can get pretty far using just the macros defined in . Hash tables can be implemented fairly quickly using arrays of SLISTs or TAILQs.

    Comment by Seth Kingsley — September 2, 2008 @ 2:26 am

  8. Of course, what I really meant was <sys/queue.h> to be escaped…

    Comment by Seth Kingsley — September 2, 2008 @ 2:26 am

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress