in reply to Re^2: Perl vs C
in thread Perl vs C

While I suspect that you were not responding to the most informed person in the world, I am willing to back up some of what he said.

According to Code Complete 2, a number of studies have shown that, to a good approximation, the amount of code written per day in a programming language is independent of how high level that programming language is. Therefore you can estimate the productivity of people working in that language from the efficiency of saying things in that language. With that in mind he produces a chart of, based on real world code bases, the approximate relative number of statements needed in different languages to say the same thing.

By his figures, Perl is 6 times as efficient to say things in as C, which is within the 5-10x estimate that Marshall gave. Incidentally by his figures, Java is 2.5x as efficient as C, which would make Perl over twice as efficient to code in as Java.

About linked lists, one of the most common use cases for linked lists in C is that someone wants a queue or a stack which can grow arbitrarily without having to know the size first. For that use case, Perl arrays are a great replacement. For the case where you wish to insert or delete in the middle of an array, it is true that a linked list written in Perl scales better than using splice. But the overhead of Perl is so much that if your arrays only have hundreds of elements in it, the simple splice solution is more efficient than writing a linked list. (There is even more overhead if you want to keep your double-linked lists from leaking memory.)

So while Perl arrays are theoretically not replacements for linked lists, for a great many practical purposes they are.

Replies are listed 'Best First'.
Re^4: Perl vs C
by halfcountplus (Hermit) on Mar 15, 2009 at 05:08 UTC
    I would have said that functionally, a linked list is more like a hash than an array, so the argument seems prone to falling off a cliff and accelerating exponentially as it enjoys the gravity.

    An irony might be that I would bet that perl hashes are implimented via C pointer arrays (just a guess).

    Another guess: 6X is bang on, bytes of code wise (but how long you took with each line may be another thing?...the only real "efficiency" may be comprehensibility, which should have to be wildly Subject-Oriented)

      Actually a linked list is orthogonal to both C arrays and Perl hashes, but is somewhat similar to Perl arrays. Given that Perl arrays are partially implemented using linked lists, that shouldn't be too much of a surprise. Although, at the end of the day, anything written in C (and Perl is written in C) uses combinations of C arrays and pointers in any case - that's pretty much all there is in C.

      It is easy to argue, because of the greater complexity arising from the greater size, that it takes longer per unit (lines, bytes, whatever) as the size increases. One could also argue that as the functional density increases per unit of size so does programming time (that's the comprehensibility thing). The implication is that somewhere there is a crossover point and a further implication that as functional density increases so does scalability. The interesting conclusion is that Perl is faster to code and scales better than C. And, because opportunity for bugs tends to be size related, Perl code likely has fewer bugs than the equivalent C code.

      Update struck bogus comment.


      True laziness is hard work
        Given that Perl arrays are partially implemented using linked lists, that shouldn't be too much of a surprise.
        That is new to me. I never got that impression from the perlguts manual page, nor from the illustrated version. Care to explain where linked lists come into play?
      You can argue until the cows come home what the appropriate Perl analogy for a linked list is. But there is no question about whether they are often used in the two use cases I presented, and about whether Perl arrays are appropriate as a replacement for that use case.

      I don't know what you meant by saying that Perl hashes are implemented via C pointer arrays. They certainly are not just internally an array. But as is normal for implementing a hash table, Perl hashes are implemented using both a C-style array for the lookup of the hash bucket from the hash value, and linked lists for the contents of each hash bucket. For the details you can look in perlguts, and for the really gory details read hv.h and hv.c in the Perl source code.

      To answer your rhetorical question about how long it takes to write Perl, multiple studies across multiple language, backed up by anecdotal experience among Perl programmers that I know, says that X lines of Perl takes roughly as long to develop as X lines of other mainstream programming languages. Furthermore comprehensibility is more an effect of the programmer rather than the language. Though if you wish to write incomprehensible code, as Larry Wall says, In accordance with UNIX philosophy, Perl gives you enough rope to hang yourself.