Perl vs C/C++ doubt...!

Posted on 2004-04-23
Last Modified: 2007-12-19
Hi Friends,

Can anyone pl. provide me with the example/reason/technical status for, why most people says that perl is SLOW than C/C++?

I like Perl, and started learning. But hearing all these I become a bit confused.....

Hope that some one will help me with a proper explanation ( which will be useful for others also having the same DOUBT).

Actually I had an ARGUMENT with some of the C/C++ friends...

Pl. HELP ME.....

Thanks & Regards,
Question by:perlstudent
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 10

Assisted Solution

rj2 earned 30 total points
ID: 10906476
C++ is compiled into machine code that runs very fast. This compilation takes some time for large programs.
Perl scripts are converted/compiled into byte code and then run.
This byte code runs reasonably fast, and faster than many other interpreted languages, but AFAIK not as fast as C/C++.
The explanation below is pasted from the O'Reilly book "Perl in a nutshell"

"[1] So do you call something a Perl "script" or a Perl "program"? Typically, the word "program" is used to describe something that needs to be compiled into assembler or byte code before executing, as in the C language, and the word "script" is used to describe something that runs through an interpreter, as in the Bourne shell. For Perl, you can use either phrase and not worry about offending anyone."

But comparing Perl and C++ this way don't really make that much sense because they are used for entirely different purposes.
Perl is very versatile and can be used for many different things, but is often used a glue language and for CGI scripts.
Perl is not used for large scale software development with milions of lines with sourcecode like C/C++.

Accepted Solution

icrf earned 160 total points
ID: 10907538
Yeah, it's Perl's versitility that makes it slower than C. In Perl, we depend on its garbage collector to free up things we're done with. In C, if you don't keep track and free them, the memory is just leaked. Keeping up with knowing what variables still have references means storing all variables in a data structures, which means adding another step to access.

Granted, most applications won't really see a huge problem with the increased time and resources (Perl does tend to solve problems by throwing memory at them). Compute-intensive applications (like scientific/accounting/gaming) fare much better in C. Lets take a quick run at finding the first hundred thousand prime numbers:

lorax: ~/prime $ cat

use constant MAX => 1e5;

my @primes = (2);

my $next = 1;
my $candidate = 3;
my ($k, $i);
while($next < MAX)
        $is_prime = 1;
        $k = sqrt($candidate);
        for($i = 0; $primes[$i] <= $k && i < $next; $i++)
                if($candidate % $primes[$i] == 0)
                        $is_prime = 0;

                $primes[$next] = $candidate;

printf("%d\n", $primes[$next-1]);

lorax: ~/prime $ cat prime.c
#include <stdio.h>
#include <math.h>

#define MAX (int) 1e5

int main(void)
        int primes[MAX], next, i, candidate, is_prime, k;
        primes[0] = 2;
        next = 1;
        candidate = 3;

        while(next < MAX)
                is_prime = 1;
                k = (int) sqrt( (double) candidate);
                for(i = 0; primes[i] <= k && i < next; i++)
                        if(candidate % primes[i] == 0)
                                is_prime = 0;

                        primes[next] = candidate;

        printf("%d\n", primes[next-1]);
        return 0;
lorax: ~/prime $ time ./
33.317u 0.028s 0:36.59 91.0%    8+3194k 0+0io 0pf+0w
lorax: ~/prime $ time ./a.out
0.875u 0.000s 0:00.98 88.7%     4+569k 0+0io 1pf+0w

Hardly a scientific test, but it shows a hefty difference. Making statement blocks into statement modifiers, pre-allocating the array, use integer; none of it made any discernable difference. Running Perl 5.8.2 on FreeBSD 4.9, everything compiled with GCC 2.95.

In the end, just go by the old Perl saying: "You can write faster code in C, but you can write code faster in Perl."
LVL 63

Assisted Solution

Zvonko earned 20 total points
ID: 10911216
Perl IS interpreter, C++ is compiler. Full stop.

(Do not let be fooled by compiled interpreter tags or JustInTime compilers; they stay to be interpreters, also when compiled)

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

ID: 10915721
C will naturally be fast but the point is for most of the applications that I code , the speed difference doesn't really make much of a difference.
So unless you are into highly complex numerical computing etc i think you can live with perl :)
And you can get a program up and running quite easily in perl.

Also with regular expressions I find perl better than C/C++ for text processing of almost any kind.


( on a sidenote I have been told that for numerical processing FORTRAN is still the best )


Expert Comment

ID: 10917198
I've been told that, too, but I've never been able to get someone to tell me why Fortran would be any faster than C.
LVL 11

Assisted Solution

avizit earned 20 total points
ID: 10917794
>> I've been told that, too, but I've never been able to get someone to tell me why Fortran would be any faster than C.<<

I googled it up a bit and came up with

there on the conclusion they have

After nearly a decade of being dismissed as too slow for scientific computing, C++ has caught up with Fortran and is giving it stiff competition

which does suggest that FORTRAN is better , but C++ ( i know you said C )  is fast catching up .. and btw that site is dated 1997 .. and things have changed quite a lot in last 7 years


coming back to C and C++ vs perl . I would also tend to believe that at least for text processing applications  perl codes would be shorter than corresponding C/C++ codes  .. and there have been reports which suggests that "whatver be the language you code in , the number of bugs per line of code remains more or less the same" . So I guess this also speaks in favour of perl.



Expert Comment

ID: 10923448
I pretty much lump C and C++ together in my head, so no matter.

Thanks for the Fortran link, I'm sending it off to my ME friends who are being forced to use Fortran because they're told it's "better".

Assisted Solution

anteusz earned 20 total points
ID: 11029993
It is easier to test ideas in Perl than in C because it takes shorter time to write a Perl program (they are compact, no need for type declaration.). And the perl programs start up faster. So if you have an idea about an algorithm , then it is easier to try them with Perl.
Yes, C is possibly faster but in many cases, the time diference does not matter at least for me.
It is easier to write a pattern matching program in Perl than in c because  regular expressions are in-built in Perl.
(It may be hard to learn how to use regular expressions.)
Also, if you want to keep your program hidden from prying eyes then C does that better.
It is pity that byte encoding of Perl scripts are not supported decently unlike in Python or Java or Php.

On the other hand, Perl program are shorter than C programs. This is a property of script like languages I guess.
E.g. I wrote a pl/sql parser/beautifier with 17kbytes. If I bundled it with a Perl environment, it would occupy 800k+17k space. How much space would occupy a similar C program? I guess much more.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question