in reply to Is Using Threads Slower Than Not Using Threads?
Absolutely, unless there is some advantage to be gained by running computationally intensive tasks in parallel. Running I/O bound tasks in parallel almost never offers any time advantage (although sometimes there can be an advantage in terms or code structure).
However, it is very often the case that a smarter algorithm can provide a substantial performance improvement. For example, in the case of the code you show it maybe that you could use a hash to test or an IP match. Consider:
use strict; use warnings; my @allIpsList = qw(10.1.1.10 10.1.1.17 10.1.1.125); my %ipsMatch = map {$_ => 1} @allIpsList; my $testFile = <<TESTDATA; 10.1.1.17 10.1.1.23 10.1.1.79 10.1.1.125 TESTDATA open my $fh, '<', \$testFile; my @found = check_fw_logs (\%ipsMatch, $fh); print "@found\n"; sub check_fw_logs { my ($ipsMatch, $fh) = @_; my @matched; while (defined (my $line = <$fh>)) { next if $line !~ /(\d+\.\d+\.\d+\.\d+)/; push @matched, $1 if exists $ipsMatch->{$1}; } return @matched; }
Prints:
10.1.1.17 10.1.1.125
For real code you would probably want to normalise the IP numbers wherever they are used and of course you'd use external files etc rather then the tricks I've used to make this a self contained example.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Is Using Threads Slower Than Not Using Threads?
by JavaFan (Canon) on Nov 01, 2010 at 11:28 UTC | |
Re^2: Is Using Threads Slower Than Not Using Threads?
by Dru (Hermit) on Nov 01, 2010 at 14:47 UTC | |
by GrandFather (Saint) on Nov 01, 2010 at 20:39 UTC | |
by BrowserUk (Patriarch) on Nov 01, 2010 at 22:08 UTC | |
by afoken (Chancellor) on Nov 02, 2010 at 17:28 UTC | |
by BrowserUk (Patriarch) on Nov 02, 2010 at 18:31 UTC |