Archived

This topic is now archived and is closed to further replies.

AndreTheGiant

efficiency of perl

Recommended Posts

Is perl efficient? I think its usually implemented in c, which is very efficient, but my perl programs seem to really eat up resources. Does it depend on the implementation? Obviously it does, but how much? I use the Active State distribution, which is pretty much the ''standard'' perl environment for windows. But I wrote a relatively simple program that was messing with reading text files and doing some string comparisons and output. The perl process took about 100 megs of memory, and ran for over an hour. I know im not the best perl programmer in the world, but I know more or less what Im doing, and I find it hard to believe that its 100% my fault the program went so slow. I dont think i could write something that slow in c if i tried. Whats the deal?

Share this post


Link to post
Share on other sites
Perl is very slow. It''s an interpereted language, so what''d you expect? A line of code is grabbed at a time and sent to the interpreter to be executed, unlike a compiled language, where the code is compiled into the machines native language to be execuated all at once.

Share this post


Link to post
Share on other sites
I believe perl now uses byte code and is not really interpreted (could be wrong). You can get an idea about how fast it is from the Computer Language Shootout.

To get an idea...

C 752
OCaml 751
C++ 743
Lisp 734
Java 703
Python 578
Perl 577
javascript 397
php 197
bash 108


[edited by - Russell on October 24, 2003 3:08:04 PM]

Share this post


Link to post
Share on other sites
quote:
I dont think i could write something that slow in c if i tried.

Faultiest conclusion of the century. Working with text in C makes me want to gouge my eyes out and probably would have taken longer to write.

Just because you''re writing in portable assembly language doesn''t make it any faster.

(Your programs might be I/O bound, too. Blaming your tools never looks good...)

Share this post


Link to post
Share on other sites
quote:
Original post by AndreTheGiant
I dont think i could write something that slow in c if i tried.



The real questions are :
- Would you be able to write it in C ?
- How long would it take you to do it ?

By you, I really mean you, AndreTheGiant, I'm not asking "Can it be done ?"

xg0blin : that's pure BS.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]


[edited by - Fruny on October 24, 2003 8:04:33 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by AndreTheGiant
Is perl efficient?



As others have noted: not really, but better than some.

quote:
I think its usually implemented in c, which is very efficient, but my perl programs seem to really eat up resources.



Almost always iirc. The key work here is "my". Not all perl scripts will eat up resources, but they usually will simply because the coder will not be terribly efficient in using what''s available to them...

quote:

Does it depend on the implementation? Obviously it does, but how much? I use the Active State distribution, which is pretty much the ''standard'' perl environment for windows.



As with all things, of course it depends on the implementation, in this case though, not much.

quote:

But I wrote a relatively simple program that was messing with reading text files and doing some string comparisons and output. The perl process took about 100 megs of memory, and ran for over an hour. I know im not the best perl programmer in the world, but I know more or less what Im doing, and I find it hard to believe that its 100% my fault the program went so slow.



Well, more likely than not you grabbed the entire file into an array, copied the array a time or two, did a compare/sort of some arrays [which in perl does usually make duplicates iirc] rather than examining the files on disk or per line.

Not that there''s usually anything wrong with that, but it will take a bit of ram and time.

quote:

I dont think i could write something that slow in c if i tried.

Whats the deal?


Oh sure you could :D

The real advantage of perl compared to C isn''t speed of runtime. The advantage of perl compared to C is that the text compare script you wrote in what? 30 mins in perl would probably take closer to 30 days in C to write. At that point, who cares if the perl script takes 5 minutes to run rather than 5 seconds? You saved 30 days just coding it.


Share this post


Link to post
Share on other sites
It is not very easy to analyze why your program eats resources, as we don''t know what it is. We don''t even know what task it is performing, except "string comparisons".

I am very inclined to think that you''ve done an inefficient program, which would be about as slow if you had done it equivalently in C/C++. If your program eats 100 megs, it is probably slow due to swapping and moving huge memory blocks; operations that isn''t executed by Perl code. Ie:
open(FILE, "<100_meg_file");
$buffer = <FILE>;
$buffer =~ s/something/else/g;
close(FILE);
would be just as slow, if you did the same insane thing in C/C++.

So, either show us code or explain what it does and on what data, or this discussion is pointless.

Share this post


Link to post
Share on other sites