Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Performance: Perl versus compiled programs

Reply
Thread Tools

Performance: Perl versus compiled programs

 
 
Sam Holden
Guest
Posts: n/a
 
      01-10-2004
On Sat, 10 Jan 2004 05:58:15 +0100, Tore Aursand <> wrote:
> On Sat, 10 Jan 2004 04:19:18 +0000, Walter Roberson wrote:
>>> Consider this simple code:
>>>
>>> while ( <DATA> ) {
>>> chomp;
>>> print $_ if ( /something/ );
>>> }

>
>> Useful code ifyou need allthe lines to runtogether when printed out.

>
> Except that it doesn't _necessarily_ print all the lines out together.


All the lines that are printed out are run together. Which is clearly
what was meant by "all" the lines.

--
Sam Holden
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      01-10-2004

(Walter Roberson) wrote:
> In article <>,
> Tore Aursand <> wrote:
>
> :Consider this simple code:
>
> : while ( <DATA> ) {
> : chomp;
> : print $_ if ( /something/ );
> : }
>
> Useful code ifyou need allthe lines to runtogether when printed out.


That depends on the value of $\.

Ben

--
Heracles: Vulture! Here's a titbit for you / A few dried molecules of the gall
From the liver of a friend of yours. / Excuse the arrow but I have no spoon.
(Ted Hughes, [ Heracles shoots Vulture with arrow. Vulture bursts into ]
/Alcestis/) [ flame, and falls out of sight. ]
 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      01-10-2004
On Sat, 10 Jan 2004 05:17:30 +0000, Sam Holden wrote:
>>>> Consider this simple code:
>>>>
>>>> while ( <DATA> ) {
>>>> chomp;
>>>> print $_ if ( /something/ );
>>>> }


>>> Useful code ifyou need allthe lines to runtogether when printed out.


>> Except that it doesn't _necessarily_ print all the lines out together.


> All the lines that are printed out are run together.


Yes. All the lines that _are printed_ will be run together. What's the
problem, actually?


--
Tore Aursand <>
"The purpose of all war is ultimately peace." -- Saint Augustine
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      01-10-2004

Actually there's another substantive comment to be made on this topic,
which perhaps calls for a separate f'up; but first:

On Fri, 9 Jan 2004, Stuart Moore wrote:

> Alan J. Flavell wrote:
>
> > I don't myself recall a situation where an application was ever turned
> > from being impractical into feasible merely by recoding in a different
> > language.

[...]

> I've had one: we were trying to write a gui front end to some perl
> command line tools. [...]


OK, point taken. Maybe I should have stressed more clearly that I was
reporting from personal experience, and didn't mean to imply that a
case might not be found if one looked hard enough.

On the other hand, I do recall a formative experience, even though
this was a considerable time back (does 360/91 ring any bells?). We
were developing a program where it was painfully obvious where the
bottleneck was going to be: the central part of the program was
inspecting thousands of co-ordinate triplets, and trying to
reconstruct track candidates in space. So in a fit of premature
optimisation, one of the programmers hand-coded that relatively small
amount of code in Assembler.

It turned out that the FORTRAN optimiser produced significantly more-
efficient code than the Assembler programmer. There were marginal
savings possible by hand-optimising the code that had been produced by
the compiler, although most attempts at optimization made the code
worse (due to the effects of pipelining in the 360/91, which the
optimising compiler knew very well how to exploit).

So the bottom line on *that* project was:

Hand-coded: worst
Compiler-generated: rather good
Hand-optimised: only slightly better, after a lot of effort

> Perl is very good at text processing, which is (presumably) what the OP
> wants, so it's unlikely that another language would get significantly
> better.


I've used Perl successfully for lots of applications that wouldn't
really count as "text processing". But I see your point.

cheers
 
Reply With Quote
 
Alan J. Flavell
Guest
Posts: n/a
 
      01-10-2004

The other point that I had been meaning to raise was this:

| Subject : Performance: Perl versus compiled programs

Only one of the replies, as far as I can see, has chosen to comment on
this point, and that in a somewhat low-key fashion.

The questioner should be aware that Perl _is_ in a sense compiled
(into an intermediate code). In practice it's usually run as
compile-and-go, rather than in separate compilation/save-object and
run-saved-object steps. So it isn't a straight comparison between a
run-time interpretative language on the one hand, and a pre-compiled
binary on the other hand. Indeed, nowadays so-called compiled objects
are often just a relatively small code framework, which calls up
potentially massive external dynamic libraries at run time; which
obscures the comparison even further.

Premature optimisation is bad anyway (I think this message emerges
from most of the replies on this thread): premature optimisation based
on a misunderstanding of the issues would only make the mistake worse.

all the best
 
Reply With Quote
 
ctcgag@hotmail.com
Guest
Posts: n/a
 
      01-12-2004
"Alan J. Flavell" <> wrote:
>
> I don't myself recall a situation where an application was ever turned
> from being impractical into feasible merely by recoding in a different
> language.


I have several of those. Most of the recoding was done by way of
Inline::C. I love that module.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service New Rate! $9.95/Month 50GB
 
Reply With Quote
 
Bill
Guest
Posts: n/a
 
      01-13-2004
(Yash) wrote in message news:<. com>...
> I have a PERL program that reads from a large text file and does some
> processing on every line it reads. If this same program is written in
> C or any other compiled language, would I get significant performance
> improvement?
> I am primarily asking about whether writing a program in Perl can
> cause significant performance impact.
>
> Thanks
> Yash


C will usually beat perl hands down. In this simple example C is over
3 times faster.

#include <stdio.h>
int main(void) {
int i=0;
char line[BUFSIZ+1];
while(fgets(line, BUFSIZ, stdin) != NULL) {
i++;
}
printf ("-> %d\n", i);
return 0;
}


time ./a.out < /usr/dict/words
-> 230534

real 0m0.167s
user 0m0.160s
sys 0m0.000s


----------------------------------

#!/usr/local/bin/perl
use strict;
my $i=0;
while(<STDIN>) {
$i++;
}
print "-> $i\n";


localhost:~$ time ./try.pl < /usr/dict/words
-> 230534

real 0m0.548s
user 0m0.520s
sys 0m0.010s
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      01-13-2004
In article <>,
Bill <> wrote:
:C will usually beat perl hands down. In this simple example C is over
:3 times faster.

Your simple example has a fair bit of startup time on the perl side.
You need to factor that out to figure out the relative execution
speeds over longer programs.
--
Disobey all self-referential sentences!
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-13-2004
>>>>> "WR" == Walter Roberson <> writes:

WR> In article <>,
WR> Bill <> wrote:
WR> :C will usually beat perl hands down. In this simple example C is over
WR> :3 times faster.

WR> Your simple example has a fair bit of startup time on the perl side.
WR> You need to factor that out to figure out the relative execution
WR> speeds over longer programs.

easily done by printing the time info from inside the perl script.

and remove the $i++ stuff from perl as it is doing that count with
$. already.

benchmarking is an art as much as anything else.

uri

--
Uri Guttman ------ -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Martien Verbruggen
Guest
Posts: n/a
 
      01-13-2004
On 12 Jan 2004 18:25:24 -0800,
Bill <> wrote:
> (Yash) wrote in message news:<. com>...
>> I have a PERL program that reads from a large text file and does some
>> processing on every line it reads. If this same program is written in
>> C or any other compiled language, would I get significant performance
>> improvement?


That entirely depends on what you need to do. Text manipulation in
Perl is generally very fast and it is often not trivial to write a set
of tools that will outperform most of Perl's text manipulation.

>> I am primarily asking about whether writing a program in Perl can
>> cause significant performance impact.


Yes it can. If you use Perl for things it wasn't designed for. Or if
you need to start up your program many, many times for very small run
times (although there are ways around that).

> C will usually beat perl hands down. In this simple example C is over
> 3 times faster.


That depends entirely on your processing and how much of Perl's
internal trickery you can use.

Apart from that, Perl programs generally have some startup cost
(parsing and compiling) that you need to factor out. If you have a
program that spends a large amount of time (relative to the startup
time) processing text, Perl might actually beat a C program.

There are many things that Perl is really, really fast at, for which
you would have to write large amounts of C code to achieve the same
speed. The equivalent Perl program is likely to have many fewer lines
of code, especially if builtin regular expressions, grep, map and
other Perl niceties can be used.

> #include <stdio.h>
> int main(void) {
> int i=0;
> char line[BUFSIZ+1];
> while(fgets(line, BUFSIZ, stdin) != NULL) {
> i++;
> }
> printf ("-> %d\n", i);
> return 0;
> }


This of course, is trivial stuff. You have fixed your input line
length, and don't correctly deal with possibly longer lines. You're
not manipulating the contents of the line buffer in any way (even
determining the length of the string, or replacing parts of the text
with something else would possibly already be slower than the Perl
equivalents). Perl strings are much smarter than C strings, and,
depending on what you do, can outperform certain operations on them.
length($buffer) is much faster than strlen(buffer) in the general
case, and O(1) instead of O(n). For long strings, this matters a lot.
The s/// operation in Perl is fast, and probably no slower than using
another regular expression library (or even Perl's own), and manually
replacing things; however, the amount of code to write and maintain is
much, much smaller.

You don't factor out the startup cost for the Perl program. Only
if your program has to run very often for small files would that
startup cost be important. If your program has to run once, for a
large file, the startup cost is likely to be insignificant.

In other words: It all depends, but your example is not at all
representative, or in any way indicative of what the OP could
realistically expect.


I generally use Perl first. If I then decide that there are
performance problems that can't trivially be fixed by using decent
hardware, I see whether I can extract some of the code, and rewrite
the slow bits in XS or C (with Inline::C), or maybe move part of the
processing to a server component that's faster. Writing everything in
C (or another machine-compiled language) is something I generally only
consider for soemthing that I know, beforehand, is time-critical, and
for which I can predict that Perl will be too slow, e.g. numerical
computations (even though there are modules for many computation
intensive things, like PDL).


Without a lot more information, it is absolutely impossible to predict
whether Perl or C will run faster, for the OP. It is likely that the
amount of development time the OP has to spend, will be much smaller
with Perl, however, and time-critical parts of the code could still be
written in C.


Martien
--
|
Martien Verbruggen | +++ Out of Cheese Error +++ Reinstall
Trading Post Australia | Universe and Reboot +++
|
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mozilla versus IE versus Opera versus Safari Peter Potamus the Purple Hippo Firefox 0 05-08-2008 12:56 PM
If I create a page, then it's compiled upon first request, where cani find the compiled code?? lander ASP .Net 5 03-05-2008 04:34 PM
equal? versus eql? versus == versus === verus <=> Paul Butcher Ruby 12 11-28-2007 06:06 AM
g++ compiled C++ code called from gcc compiled C code Klaus Schneider C++ 1 12-02-2004 01:44 PM
compiled dll's versus aspx.cs =?Utf-8?B?Ry5ILkxhd3JlbmNl?= ASP .Net 2 04-07-2004 09:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57