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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.