Revisiting Perl Regular Expressions Test

Michael Marr By Michael Marr
Expert Author
Article Date:

So a few weeks ago, I decided to put Perl’s Regular Expression power to the test. The test was admittedly a quickly put together test, and thus had some flaws. I wanted to go back and revisit some of those flaws and re-evaluate Perl’s status after I’ve cleared up these missteps.

Perl was, on average, approximately twice as fast as PHP for the same regular expressions on the same source files. However, I realized afterwards that my code in PHP was flawed: it opened the complete file into memory before processing the regex line-by-line. The Perl code simply set a pointer to the start of the file and went line-by-line, thus never loading the full file into memory. In the smaller filesize tests, this likely didn’t affect it too much. In the case of 1 gig source file, this clearly had an effect. I revamped the PHP code to the following:

$file = fopen("haystack_1gig.txt",  'r');
  . . .
  while ( !feof($file) )
  {
  preg_replace("/$search/i", $replace, fgets($file), -1, $x);

This now mirrored how my Perl code handled the haystack file, so I re-ran the tests (in the same manner as before) with the following results:

Search

Filesize

Execution Time in Seconds

Perl

PHP

10 kb

0.003

0.014

100 kb

0.004

0.019

1 mb

0.02

0.069

1 gb

14.581

49.686

Search and Replace

Filesize

Execution Time in Seconds

Perl

PHP

10 kb

0.014

0.014

100 kb

0.014

0.020

1 mb

0.016

0.070

1 gb

14.279

43.409

Although PHP did see a significant decrease in execution time across the board (and especially for the 1 gig file), Perl still handily defeats PHP in these tests.

The other major flaw is that I only tested Perl against PHP. As noted in my original article, this was due to the fact that I had a project where I had to choose between the two languages, and thus I didn’t include others in my test. Equally important in the decision not to include other languages is my lack of knowledge in other languages. Feel free to submit some versions of this code in other languages, and I’ll run them in the same manner and compile the results.

About Michael Marr
Michael Marr is a IT staff Writer for WebProNews.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • 160×600
  • 152×252
  • Newsletter Signup
    Get The Email Newsletter! Please subscribe using your company email address
  • 336×280
  • 336×130