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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:47:28+00:00 2026-06-02T19:47:28+00:00

@solved C# with the same code is twice as fast i am parsing a

  • 0

@solved C# with the same code is twice as fast

i am parsing a phred33 fastq file in perl and it is taking a considerable amount of time (on the order of 15 minutes). The fastq file is about 3 gigs.
Are there any reasonable ways to make this faster?

$file=shift;
open(FILE,$file);
open(FILEFA,">".$file.".fa");
open(FILEQA,">".$file.".qual");
while($line=<FILE>)
{
    chomp($line);
    if($line=~m/^@/)
    {


    $header=$line;
    $header =~ s/@/>/g;
    $seq=<FILE>;
    chomp($seq);
    $nothing=<FILE>;
    $nothing="";
    $fastq=<FILE>;

    print FILEFA $header."\n";
    print FILEFA $seq."\n";
    $seq="";
    print FILEQA $header."\n";

        @elm=split("",$fastq);
        $i=0;
        while(defined($elm[$i]))
        {
            $Q = ord($elm[$i]) - 33;
            if($Q!="-23")
            {
            print FILEQA $Q." ";
            }
            $i=$i+1;
        }
        print FILEQA "\n";
    }
}
print $file.".fa\n";
print $file.".qual\n";
  • 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-02T19:47:31+00:00Added an answer on June 2, 2026 at 7:47 pm

    There’s next to no CPU being used here. it’s IO bound, so it’s mostly the time to read through 3GB. There are micro-optimisations (and other cleanups) that can be done.

    First, always use use strict; use warnings;.

    The main code is

    my @elm = split(//, $fastq);
    my $i=0;
    while(defined($elm[$i])) {
        my $Q = ord($elm[$i]) - 33;
        if($Q!="-23") {
            print FILEQA $Q." ";
        }
        $i=$i+1;
    }
    

    The purpose of if($Q!="-23") is to check if the character is a newline, which you wouldn’t have to do if you did chomp($fastq);. (What’s with the quotes around -23?!)

    chomp($fastq);
    my @elm = split(//, $fastq);
    my $i=0;
    while(defined($elm[$i])) {
        my $Q = ord($elm[$i]) - 33;
        print FILEQA $Q." ";
        $i=$i+1;
    }
    print FILEQA "\n";
    

    Using a while loop just complicates things. Use a for loop when you have a known number of iterations.

    chomp($fastq);
    for (split(//, $fastq)) {
        print FILEQA (ord($_)-33)." ";
    }
    print FILEQA "\n";
    

    It might help a bit to turn that inside out.

    chomp($fastq);
    print FILEQA join(' ', map ord($_)-33, split //, $fastq), "\n";
    

    On second thought, not inside out enough 🙂

    $fastq =~ s/(.)/(ord($1)-33) . " "/eg;
    print FILEQA $fastq;
    

    But what it we precalculated the translations? Then we wouldn’t have to call a sub (the /e code) repeatedly.

    my %map = map { chr($_) => ($_-33)." " } 0x00..0xFF;
    
    $fastq =~ s/(.)/$map{$1}/g;
    print FILEQA $fastq;
    

    After a bit more cleanup, we get:

    use strict;
    use warnings;
    
    my %map = map { chr($_) => ($_-33)." " } 0x00..0xFF;
    
    my $file = shift;
    
    my $fa_file   = "$file.fa";
    my $qual_file = "$file.qual";
    
    open(my $FILE,   '<', $file     ) or die $!;
    open(my $FILEFA, '>', $fa_file  ) or die $!;
    open(my $FILEQA, '>', $qual_file) or die $!;
    
    while (my $header = <$FILE>) {
        next if $header !~ /^@/;
    
        my $seq = <$FILE>;
        <$FILE>;
        my $fastq = <$FILE>;
    
        $header =~ s/@/>/g;
        $fastq =~ s/(.)/$map{$1}/g;
    
        print $FILEFA $header;
        print $FILEFA $seq;
    
        print $FILEQA $header;
        print $FILEQA $fastq;
    }
    
    print "$fa_file\n";
    print "$qual_file\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just solved another problem regarding the same piece of code but I
This code is just to realize a function that when you input twice same
SOLVED. Code has been edited to reflect solution. Given the following GridView : <asp:GridView
Solved Problem: to show external code dependency by submodules: Thanks to VonC ! Current
In frame 1 of a very simple flash file, I have the following code:
* SOLVED * Answer is in separate post below This code runs fine in
as I asked time ago in this question , I solved my problem using
I have this code. From main function i twice call sportPrisevinners function and if
Trying to solve this one for a long time, jsfiddle wont re-produce this code
Solved I actually found out what is going on here. Turns out it was

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.