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.
use vars qw($myvar @results $a $b $c $d);
$myvar = "one,two,three,four";
Regex => '($a=$1, $b=$2, $c=$3, $d=$4) if $myvar =~ /^([^,]+),([
Split => '@results = split /,/, $myvar'
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.