Hello Monks,

Recently I was introduced to diamond operator from I/O Operators, through the thread (How to amend character count.).

Well based on the answers I started wondering what is the best and fastest way to extract data from files with thousands of lines. So I was trying to Benchmark the two two fastest from my opinion ways (while() and grep) in iterating files.

I did not think it would be a problem to replicate the behavior but I end up with the script hanging and doing nothing. I was not able to understand why, until I created separate sub routines.

When each sub routine is executed individually and the script exits everything works fine, but when the subroutines are executed sequentially the script stays hanged.

I can understand that is based on the @ARG but I do not know how to tell to the script that that there is no more files to process by using explicitly the diamond operator <> and not individually the @ARG

Sample of code that can replicate my problem:

#!/usr/bin/perl use strict; use warnings; use Benchmark::Forking qw( timethese cmpthese ); # UnixOS # use Benchmark qw(:all) ; # WindowsOS sub test_grep { my @matched = grep { $_ =~ /AGGT/ } <>; if (@matched) { for(@matched) { chomp $_; print "Grep: " . length($_) . "\n";} } close ARGV if eof; } sub test_while { my $count; while (<>) { if ($_ =~ /^>hsa/){ chomp (my $line = <>); $count .= length($line); $count .= " "; } } continue { close ARGV if eof; # reset $. each file } print "While: " . $count . "\n"; } # test_grep(); # Works fine separate but not combined test_while(); # Works fine separate but not combined __DATA__ $ perl test.pl test.txt Grep: 107 Grep: 251 ^C $ perl test.pl test.txt While: 107 251 ^C __END__ my $results = timethese( 2, { 'Grep' => sub { }, 'While' => sub { }, }, 'none' ); cmpthese( $results );

Sample of test.txt file with data for replication purposes:

>hsa_circ_0000001|chr1:1080738-1080845-|None|None ATGGGGTTGGGTCAGCCGTGCGGTCAGGTCAGGTCGGCCATGAGGTCAGGTGGGGTCGGCCATGAAGGTG +GTGGGGGTCATGAGGTCACAAGGGGGTCGGCCATGTG >hsa_circ_0000002|chr1:1158623-1159348-|NM_016176|SDF4 GGTGGATGTGAACACTGACCGGAAGATCAGTGCCAAGGAGATGCAGCGCTGGATCATGGAGAAGACGGCC +GAGCACTTCCAGGAGGCCATGGAGGAGAGCAAGACACACTTCCGCGCCGTGGACCCTGACGGGGACGGT +CACGTGTCTTGGGACGAGTATAAGGTGAAGTTTTTGGCGAGTAAAGGCCATAGCGAGAAGGAGGTTGCC +GACGCCATCAGGCTCAACGAGGAACTCAAAGTGGATGAGGAAA

Does any one know how to approach this minor problem?

Update: With the help of tobyink the following code demonstrates the solution to my problem but also that while is faster than grep on my loop because of the for loop that I am using as extra. Thanks everyone again for their time and effort.

#!/usr/bin/perl use strict; use warnings; use Benchmark::Forking qw( timethese cmpthese ); # UnixOS # use Benchmark qw(:all) ; # WindowsOS my @preserved = @ARGV; sub test_grep { @ARGV = @preserved; # restore original @ARGV my @matched = grep { $_ =~ /AGGT/ } <>; my $count; if (@matched) { for(@matched) { chomp $_; $count .= length($_) . " ";} } close ARGV if eof; } sub test_while { @ARGV = @preserved; # restore original @ARGV my $count; while (<>) { if ($_ =~ /AGGT/){ chomp; $count .= length($_) . " "; } } continue { close ARGV if eof; # reset $. each file } } my $results = timethese(10000000, { Grep => \&test_grep, While => \&test_while }, 'none'); cmpthese( $results ); __DATA__ perl test.pl test.txt Rate Grep While Grep 144175/s -- -21% While 182882/s 27% --

Thanks in advance for your time and effort.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to Benchmark diamond operator by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.