Tuesday, March 10, 2009

why text processing is fast in perl

why text processing is fast in perl
by anilkumar

anilkumar has asked for the wisdom of the Perl Monks concerning the following question:

hi folks ,i am new to perl, so many people says that in perl text processing is speed, why text processing is speed in perl? what is the in built mechanism that made perl text processing speed. please let me know.

Comment on why text processing is fast in perl
Re: why text processing is fast in perl
by jettero (Prior) on Jun 20, 2007 at 14:06 UTC

I'm not sure it's concerning the speed as much as it's concerning the ease.

In perl, regular expressions are given special importance that make it natural to work with patterns without having to do a lot of extra work.

UPDATE: indeed, the sed-in-place perl -i -pe 's/word/lol/' is just about the coolest thing of all.



Re^2: why text processing is fast in perl
by ikegami (Cardinal) on Jun 20, 2007 at 14:14 UTC

There are also useful command line options (-p, -n, -i) and reading a file line by line is easy (while ()). There's also support to read either from STDIN or from a file specified on the command line (while (<>)) which is great for text processing tools.

Re: why text processing is fast in perl
by suaveant (Vicar) on Jun 20, 2007 at 14:15 UTC

I'll have to agree with jettero... with Perl, it's the rapid development angle and power of the language that really makes it great. I can write things to process text in Perl at least twice as fast as any other language... probably 5 times faster in most cases.

- Ant
- Some of my best work - (1 2 3)

Re: why text processing is fast in perl
by princepawn (Parson) on Jun 20, 2007 at 14:18 UTC

I never considered Perl speed of execution as being very fast, just speed of development. However, this text processing benchmark shows Perl to be the fastest non-compiled submission, ahead of Python and Ruby.

But I dont know the technical reasons for its speed.

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Re: why text processing is fast in perl
by whereiskurt (Pilgrim) on Jun 20, 2007 at 15:55 UTC

Everyone has made a great points about Perl's true strength being 'getting stuff done.' BUT! The Computer Scientist in me wants to answer the question in a slightly different way. ;-)

Text processing can be thought of as reading, writing and matching data. Why are the reading and writing fast? I'd have to guess native bindings to standard C IO libs that can achieve the maximum native throughput with caching, buffering, etc. Why is the pattern matching so fast? ...

To fully understand that, you'll have to answer the question of 'What is computable?' and 'What are finite state automata (FSA/DFA/NFA)? ' Once you 'understand' that, you'll be on track to ask 'What are regular languages and how are regular expressions implemented Perl?' The jist of it: at any given time the regular expression engine is either in a match state or an unmatch state. Keep feeding it more input and that state could toggle. How do you define the 'matching states'? With regular languages of course....

Your question is pretty open ended, and I just wanted to throw some thoughts at you. ;-)



PS: Just to stir the pot, I'll link to this article too:

Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby, ...)

This article picks an edge case where a DFA is significantly slower than an NFA - two different ways you can implement a regular expression engine. I'm linking to this to point out that knowing what pattern you need to match could help you choose the better tool for the job.

No comments: