in reply to Re: compiling perl scripts aka why is perl not as fast as C
in thread compiling perl scripts aka why is perl not as fast as C

Define "flexible" in this context. C is a much simpler language than Perl and much easier to learn. C is however much harder to use to do simple things that in Perl you take for granted, like using the contents of a variable as a character, string, integer, float, double, pointer, ... as suits the current purpose.

Programs written in any language are buggy when you don't do things right. Strongly typed languages tend to catch more silly errors at compile time where languages like Perl tend to catch those errors at run time (if at all). On the other hand languages like C allow you to more easily generate much nastier classes of bugs than languages like Perl (bad pointers tromping over memory for example).

It seems to me the reason there is a much stronger ethos of unit testing in the Perl community than in the C community is that the balance between run time and compile time errors is quite different between the languages. Perl requires run time testing to shake out the silly typo type errors that in C are found at compile time. My guess is that per line of code typical Perl tends to pack in more bugs than typical C, just because Perl can do a heck of a lot more work in one line than C can.


True laziness is hard work
  • Comment on Re^2: compiling perl scripts aka why is perl not as fast as C

Replies are listed 'Best First'.
Re^3: compiling perl scripts aka why is perl not as fast as C
by moritz (Cardinal) on Mar 22, 2010 at 10:06 UTC
    C is a much simpler language than Perl and much easier to learn.

    That vastly depends on what you mean by learning a language, and how familiar the potential programmer is with low level concepts like computer memory.

    If she is an experienced assembler programmer, C is probably easier to learn than Perl. If she has no clue about memory, pointers, segmentation faults and the like, Perl is easier to learn.

    Also it depends on how you count: it might be easier to learn 90% of the features of the C programming language than learning 90% of the features of Perl. But the difference is that with 20% of Perl features you can already achieve a whole lot of stuff - but not with C.

    So if you count "learn enough of a language to get stuff done", I disagree that C is easier to learn than Perl.

    Perl 6 - links to (nearly) everything that is Perl 6.
      So if you count "learn enough of a language to get stuff done", I disagree that C is easier to learn than Perl.

      I definitely agree with this. C is a "short" language, but it takes a lot of training to learn how to use it effectively and well. Just because there aren't that many functions in the base language, doesn't mean that it is easy.

      Perl helps immensely on two main 'C' problems: "off-by-one" and use of unallocated or un-initialized memory! The Perl foreach(@ayx){} is magic - so much better than C for(i=0,...). Also for example, I helped a student a couple of weeks ago with an array of struct problem. Due to a defect in his malloc algorithm, sometimes one element of this array of struct wasn't being initialized. The student actually had a setup where "hey my program works on this computer A and it doesn't work on this computer B". The difference was that the "garbage" left in memory on computer A from some previous thing happened to be "0's" while on the other machine B there was something different. Perl has fewer of these "hey it worked on Tuesday, but doesn't "work" on Thursday problems.

      I read some comment in this thread about assembly vs C. As it turns out modern "C super compilers" are very, very good. I am just guessing, but maybe 10% of asm coders could beat one of these things cranked up it its highest optimization level. ASM is way harder than C or Perl and "average ASM" is likely to run slower than well written C using a good optimizing compiler! Now, I do like ASM and there is a place for this to be used!

      Anyway back to Perl and C, my experience is that coding efficiency is like 5 or 10 to one vs C. Perl runs like 1/3 speed (assuming both "smart" C code and Perl code). Whether this mix is what you see depends upon what tasks you are coding.

      I am currently tutoring some Perl, C and ASM classes. Perl is not a good beginner language. The folks who do well in Perl have good C skills (and by that I mean going way past "hello world!". In the "old days" folks started with ASM, but now the good starting place is 'C'. The folks who do well in ASM also have good C skills.

Re^3: compiling perl scripts aka why is perl not as fast as C
by chromatic (Archbishop) on Mar 21, 2010 at 21:00 UTC
    C is a much simpler language than Perl and much easier to learn.

    Surely you must be joking.

    I can teach a programming novice how to write a short program which performs IO in an hour, and he or she has a better than decent chance of remembering how things work with a notecard or two of notes.

    I can probably walk the same novice through compiling and running "Hello, world!" in that time and he or she might remember how things work.

    It seems to me the reason there is a much stronger ethos of unit testing in the Perl community than in the C community is that the balance between run time and compile time errors is quite different between the languages.

    No, it's because it's immensely easier to write tests in Perl than in C.

    My guess is that per line of code typical Perl tends to pack in more bugs than typical C, just because Perl can do a heck of a lot more work in one line than C can.

    I've heard the opposite, because Perl requires far fewer lines of code than the corresponding C. The bug defect rate tends to be constant per SLOC.

      C is a much simpler language than Perl and much easier to learn.
      Surely you must be joking.

      Not at all. But remember what the C language is without its standardish libraries - some simple control structures, a handful of data types, some arithmetic operators, some bitwise operators and some boolean operators. The core language is very small. Perl's core language on the other hand is huge and contains all sorts of subtle special magic. I agree it is harder to use C than Perl, but that is not the same as learning the language.

      No, it's because it's immensely easier to write tests in Perl than in C.

      I admit I write C++ rather than C so I don't know how hard or otherwise it is to write tests in C. However, in C++ using easily available test frameworks it's not a lot different to write test code in C++ than it is in Perl.

      The bug defect rate tends to be constant per SLOC.

      That would be my first thought too. However Perl rather encourages concatenating the odd map, grep, join, split and so on together into a single succinct line - what could be written over several lines collapses to one. Therein lies the problem with SLOC and Perl exacerbates the issue by often allowing the code to be as readable in the compact single line form as in the multiple line form.


      True laziness is hard work