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 8622207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:59:41+00:00 2026-06-12T06:59:41+00:00

I made the following Perl script to handle some file manipulation at work, but

  • 0

I made the following Perl script to handle some file manipulation at work, but it’s running far too slowly at the minute to be put in production.

I don’t know Perl very well (not one of my languages), so can someone help me identify and replace parts of this script that would be slow given it’s processing ~40 million lines?

Data being piped in is in the format:

col1|^|col2|^|col3|!|
col1|^|col2|^|col3|!|
... 40 million of these.

The date_cols array is calculated before this part of the script and basically holds the index of columns containing dates in the pre-converted format.

Here’s the part of the script that will be executed for every input row. I’ve cleaned it up a little and added comments, but let me know if anything else is needed:

## Read from STDIN until no more lines are arailable.
while (<STDIN>)
{       
    ## Split by field delimiter
    my @fields = split('\|\^\|', $_, -1);   

    ## Remove the terminating delimiter from the final field so it doesn't
    ## interfere with date processing.
    $fields[-1] = (split('\|!\|', $fields[-1], -1))[0];

    ## Cycle through all column numbres in date_cols and convert date
    ##  to yyyymmdd
    foreach $col (@date_cols)
    {
        if ($fields[$col] ne "")
        {
            $fields[$col] = formatTime($fields[$col]);
        }
    }

    print(join('This is an unprintable ASCII control code', @fields), "\n");
}           

## Format the input time to yyyymmdd from 'Dec 26 2012 12:00AM' like format.
sub formatTime($)
{
    my $col = shift;        

    if (substr($col, 4, 1) eq " ") {
        substr($col, 4, 1) = "0";
    }       
    return substr($col, 7, 4).$months{substr($col, 0, 3)}.substr($col, 4, 2);
}
  • 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-06-12T06:59:42+00:00Added an answer on June 12, 2026 at 6:59 am

    If written purely for efficiency, I’d write your code like this:

    sub run_loop {
      local $/ = "|!|\n"; # set the record input terminator
                          # to the record seperator of our problem space
      while (<STDIN>) {       
        # remove the seperator
        chomp;
    
        # Split by field delimiter
        my @fields = split m/\|\^\|/, $_, -1;
    
        # Cycle through all column numbres in date_cols and convert date
        #  to yyyymmdd
        foreach $col (@date_cols) {
          if ($fields[$col] ne "") {
            # $fields[$col] = formatTime($fields[$col]);
            my $temp = $fields[$col];
            if (substr($temp, 4, 1) eq " ") {
              substr($temp, 4, 1) = "0";
            }       
            $fields[$col] = substr($temp, 7, 4).$months{substr($temp, 0, 3)}.substr($temp, 4, 2);
          }
        }
        print join("\022", @fields) . "\n";
      }
    }
    

    The optimizations are:

    • Using chomp to remove the |!|\n string at the end
    • Inlining the formatTime sub.

    Subroutine calls are extremely expensive in Perl. If subs have to be used very efficiently, prototype checking can be disabled with the &subroutine(@args) syntax. If @args are ommited, the current arguments @_ are visible to the called sub. This can lead to bugs or additional performance. Use wisely. The goto &subroutine; syntax can be used as well, but this meddles with return (basically a tail call). Do not use.

    Further optimizations could include removing the hash lookup %months, as hashing is expensive.

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

Sidebar

Related Questions

I made the following small Javascript script to enable some form elements on my
I have made following code, but whatever I type it will always print some
I get FC when running but it compiles. NOTE: Changes have been made following
I've made following protection for my variables: $ad_title=htmlentities($ad_title); $ad_title=mysql_real_escape_string($ad_title); $ad_title=stripslashes($ad_title); But every time I
I have the following, simplest Perl CGI script: use strict; use warnings; use CGI();
I need to validate phone number in PHP. I have made following regex, but
I have made following relative layout in an xml file lets say add_relative_layout.xml <?xml
I'm working on a simple Perl script to monitor a log file using the
I made the following website: www.appliedintelligentmarketing.com It works fine in all browsers but I've
I have made following changes to the POM.xml file for adding a manifest file

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.