ccelt09 has asked for the wisdom of the Perl Monks concerning the following question:
This program counts the occurrences of a single number in a very long string, 200e6 characters long. The first run through counted from start until 2.7e6 characters and returned an output in about twelve seconds. I figured running from 2.76e6 to finish would take no more than 12 x 100 seconds ~ 20 minutes. It's actually on hour 2 and seems to have flatlined. This seems unnecessarily long given the scope of the program. Any thoughts on improving this code?
Can it be done without storing the input file as a scalar or converting it to an array?
#!/usr/bin/perl -w use strict; use warnings; #--------------- my $total_filtered = 0; my $input_file = "$input_dir"."input".".txt"; open(CGS, "<$input_file") or die "can't open $input_file\n"; my $cgs = <CGS>; my $substring = substr($cgs, 2700000); my @array = split(//,$substring); foreach my $line (@array){ if($line =~ /0/){ $total_filtered++; } } print "$total_filtered\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Improving Efficiency
by BrowserUk (Patriarch) on Aug 31, 2013 at 12:32 UTC | |
|
Re: Improving Efficiency
by hdb (Monsignor) on Aug 31, 2013 at 12:35 UTC | |
by Anonymous Monk on Sep 01, 2013 at 18:36 UTC | |
|
Re: Improving Efficiency
by Corion (Patriarch) on Aug 31, 2013 at 13:11 UTC | |
by Anonymous Monk on Aug 31, 2013 at 13:22 UTC | |
by hdb (Monsignor) on Aug 31, 2013 at 13:42 UTC | |
|
Re: Improving Efficiency
by xiaoyafeng (Deacon) on Sep 01, 2013 at 14:20 UTC |