in reply to Why is this code so much slower than the same algorithm in C?

Let's look at what is really going on when these programs run.

  1. First the C program. It translates into the following assembler:
  2. Now the Perl code. It translates into the following ... um...I terrified to apply a term to this because one or more of the local language laywers is going to leap all over what ever term I use to say that it isn't really 'term' because blah, blah, ... more irrelavancies, ... blah!

    So, whatever terms suits you, which may include one of the following: <opcodes|bytecode|syntax tree|abstract syntax tree|other>.

    (Dodge issue!) This is the output from perl -MO=Concise YourScript.pl:

    And within that, there is one line: o                             <2> modulo[t5] sKP/2 ->p

    which is the equivalent of these four assembly instructions from the C version:

    ; Line 8 mov eax, DWORD PTR _i$[ebp] cdq idiv DWORD PTR _j$[ebp] test edx, edx

Now let's look at the assembler that sits behind that perl modulo instruction:

So the answer to your question is: when you understand why those four instructions in the C version, require those 700+ lines of assembler for the perl version, then you'll understand why the performance difference exists.

And also why it isn't a problem!


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."