Solved

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

Posted on 2004-04-23
8
504 Views
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,
PS
0
Comment
Question by:perlstudent
8 Comments
 
LVL 10

Assisted Solution

by:rj2
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++.
0
 
LVL 2

Accepted Solution

by:
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 prime.pl
#!/usr/local/bin/perl

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;
                        last;
                }
        }

        if($is_prime)
        {
                $primes[$next] = $candidate;
                $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;
                                break;
                        }
                }

                if(is_prime)
                {
                        primes[next] = candidate;
                        next++;
                }
                candidate++;
        }

        printf("%d\n", primes[next-1]);
        return 0;
}
lorax: ~/prime $ time ./prime.pl
1299709
33.317u 0.028s 0:36.59 91.0%    8+3194k 0+0io 0pf+0w
lorax: ~/prime $ time ./a.out
1299709
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."
0
 
LVL 63

Assisted Solution

by:Zvonko
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)

0
 
LVL 11

Expert Comment

by:avizit
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.

/abhijit/


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

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:icrf
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.
0
 
LVL 11

Assisted Solution

by:avizit
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

http://osl.iu.edu/~tveldhui/papers/DrDobbs2/drdobbs2.html

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.

/abhijit/

0
 
LVL 2

Expert Comment

by:icrf
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".
0
 
LVL 1

Assisted Solution

by:anteusz
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.
 
 
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now