Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1081227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:08:32+00:00 2026-05-16T22:08:32+00:00

Is there any way I could optimize the following script to run faster? foreach

  • 0

Is there any way I could optimize the following script to run faster?

foreach my $arg (@data){ #  
   @score=();
   `program $arg $arg1 > $result`; #!!! $arg1 is a very large file with lots of data!!!
   open(FH,$result);
   while(<FH>){
      chomp;
      if($_ =~ /\d+.+\s+(\d+\.\d+|\d+\.|\.\d+).+/){ #here i'm looking for any number such as: 21.343 or 12 or 0.22 or -3.0
         push(@score, $1);
      }
   }
   close FH;
   @sorted = sort{$a <=> $b} @score; #a sorted score is what i actually want
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:08:33+00:00Added an answer on May 16, 2026 at 10:08 pm

    There are a few things I can see (for instance not loading your result into the file immediately), but I suspect the main performance benefit you will get will probably be from using a different regex. To that end, do you have a better idea what the data output format from your program is?

    Here’s some sample perl that may run a little bit quicker:

    use strict;
    foreach my $arg (@data){
      my @score=();
      open(my $fh, "program $arg $arg1 |");
      while (<$fh>) {
        chomp;
        if (/\d+.+\s+((\d+)?\.?\d+)/o) {
          push(@score, $1);
        }
      }
      close($fh);
      my @sorted = sort { $a <=> $b } @score;
    }
    

    Notice a few things here:

    1. I’m using a program file handler so that I’m not using a temporary file, thus skipping a whole pass of data.
    2. I changed the regex to use nested groups rather than multiple options.
    3. I use strict and keep package names (for the love of God use strict in your perl).

    The other people have said to use threads. You DO NOT need to do this, as running the process as I have done with the trailing pipe (|) in the open function causes perl to fork a process for you. Then you use standard unix pipes to read from the program asynchronously.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I could run the following 'logical code' to actually work?
Is there any way that I could get the source of a website (as
Is there any way that I could enjoy a decodeValue() function in PHP, too?
I would like to know if there is any way that I could build
Is there any way, in any language, to hook my program when a user
There is unsurprisingly a run time exception thrown by the following code : data
is there any faster way to parse a text than by walk each byte
Is there any way to check whether a file is locked without using a
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there any way to apply an attribute to a model file in ASP.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.