I know that
gryng has stated that regex is faster than a split for long lines. However, his test case was
$testlarge = "a " x 100000;. That's a two-hundred thousand character line. I'm highly inclined to doubt that
most people are going to be working with lines of that length. In the real world (no offense to
gryng intended), where we're not splitting one hundred thousand repetitions of "a ", we usually use much smaller chunks of data. For those, split is probably the best best.
You might want to consider using Benchmark to figure out what works best. I looked at your regex and realized that after you added the capturing parens and assigned the $digit variables, you were looking at a significant performance hit. You can use Benchmark to analyze these things a bit more carefully.
#!/usr/bin/perl -w
use strict;
use Benchmark;
use vars qw($myvar @results $a $b $c $d);
$myvar = "one,two,three,four";
timethese(1000000, {
Regex => '($a=$1, $b=$2, $c=$3, $d=$4) if $myvar =~ /^([^,]+),([
+^,]+),([^,]+),([^,]+)$/',
Split => '@results = split /,/, $myvar'
});
This produced:
Benchmark: timing 1000000 iterations of Regex, Split...
Regex: 27 wallclock secs (28.06 usr + 0.00 sys = 28.06 CPU)
Split: 16 wallclock secs (16.19 usr + 0.00 sys = 16.19 CPU)
In this case,
split was clearly the winner. I'd be interested in seeing some sample data and a code snippet to see how you're getting a regex to outperform a split. The structure of the data is
everything when it comes to crafting an efficient regex.
Cheers,
Ovid
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.