in reply to Re: Re: Re: Alternatives to split?
in thread Alternatives to split?

Sorry all should have added..

Have done the split on first field
($tag)=split (/\|/,$data,2);
as well as substr and index stuff
$tag=substr ($data,0,index($data,'\|',));
I then benchmarked them
Benchmark: timing 1000000 iterations of split, substr... split: 8 wallclock secs ( 6.93 usr + 0.00 sys = 6.93 CPU) @ 14 +4279.32/s(n=1000000) substr: 1 wallclock secs ( 1.54 usr + 0.00 sys = 1.54 CPU) @ 64 +8088.14/s(n=1000000)
should split be slow compared to substr?
Thinking about what i really want to know monks...
Once i ahve the first field is there a quick way of spliting this field on first space then on an undersocre (if it exsits)?
In my test file I have 5161033 lines, split slows down processing dramatically. When more substr are introduced processing slows down agian!

Replies are listed 'Best First'.
5Re: Alternatives to split?
by jeffa (Bishop) on Sep 03, 2003 at 13:26 UTC
    If the line you are splitting is very long, then yes, split will be slower than substr. To answer your question, how about something like:
    use strict; use warnings; while (<DATA>) { my $index = index($_,'|'); next unless $index; # add error checking!! my $tag = substr($_,0,$index); expensive_function($tag,$_) if $tag =~ / /; } sub expensive_function { my ($tag,$line) = @_; my ($label,$number) = split(' ',$tag,2); my ($digit) = $label =~ /_(\d+)$/; my @field = split(/\|/,$line); # now do something with all of this ... } __DATA__ START_1 123| FILE 2222| XXXX| AAAA| NEW | END_1|
    This is, of course, complete guess-work. You really should take the time to explain what it is you are trying to accompish - we know that you want to speed your application up, but what does your application do? Take the time to carefully explain yourself and get better answers. I can tell you are in a hurry because you have a plethora of typos. Take a look at How (Not) To Ask A Question.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)