Henry Law
2017-07-05 14:50:13 UTC
I searched for this because if I discern a problem then surely someone
else must have too, but found nothing; which makes me wonder whether I'm
barking up the wrong tree.
I wrote a simple test program which copies the contents of a directory
(I use images because they're 3 or 4MB each) into a temporary directory;
a parameter allows me to choose File::Copy operations or using "system"
with the "cp" command. (The source is at the bottom in case anyone
wants it). Here are some results:
***@eris ~/tools $ ./testcopy p '/d/u/Photos/2017_06_07'
157 files copied using perl calls in 9.43216 seconds
***@eris ~/tools $ ./testcopy s '/d/u/Photos/2017_06_07'
157 files copied using system calls in 3.445381 seconds
This suggests that, despite shelling out to a system command each time,
system(cp ....) is near enough three times as fast, which I wouldn't
have expected.
Does this tie in with other people's experience? Is there any reason
why I shouldn't re-code my programs to use 'cp' rather than 'File::Copy'?
-------------------------------------------------
#! /usr/bin/perl
# testcopy method dir
#
# Where method: p or s, meaning "via perl" or "via system"
# dir: a directory full of test files
#
# files are copied to a temporary directory, which is then deleted
use strict;
use warnings;
use 5.010;
use File::Temp qw( tempdir );
use File::Copy;
use Time::HiRes qw( gettimeofday tv_interval );
my $method = shift or die usage();
$method = lc $method;
die "Invalid method '$method'\n" unless $method eq 'p' || $method eq 's';
my $source = shift or die usage();
die "'$source' is not a directory\n" unless -d $source;
$source =~ s|/$||g;
my $target = tempdir( CLEANUP => 1 );
opendir my $DIR, $source or die "Couldn't open '$source':$!";
my $count = 0;
my $start = [ gettimeofday() ];
while( my $file = readdir $DIR ){
my $fullname = "$source/$file";
next if $file eq '.' || $file eq '..' || -d $fullname;
if ( $method eq 'p' ){
copy $fullname, $target;
}
else {
system( 'cp', $fullname, $target );
}
$count++;
}
my $elapsed = tv_interval( $start );
print "$count files copied using " . ($method eq 'p'? 'perl':'system') .
" calls in $elapsed seconds\n";
closedir $DIR;
sub usage{
my $usage = <<ENDTEXT;
Usage:
$0 { p | s } directory
ENDTEXT
return $usage;
}
else must have too, but found nothing; which makes me wonder whether I'm
barking up the wrong tree.
I wrote a simple test program which copies the contents of a directory
(I use images because they're 3 or 4MB each) into a temporary directory;
a parameter allows me to choose File::Copy operations or using "system"
with the "cp" command. (The source is at the bottom in case anyone
wants it). Here are some results:
***@eris ~/tools $ ./testcopy p '/d/u/Photos/2017_06_07'
157 files copied using perl calls in 9.43216 seconds
***@eris ~/tools $ ./testcopy s '/d/u/Photos/2017_06_07'
157 files copied using system calls in 3.445381 seconds
This suggests that, despite shelling out to a system command each time,
system(cp ....) is near enough three times as fast, which I wouldn't
have expected.
Does this tie in with other people's experience? Is there any reason
why I shouldn't re-code my programs to use 'cp' rather than 'File::Copy'?
-------------------------------------------------
#! /usr/bin/perl
# testcopy method dir
#
# Where method: p or s, meaning "via perl" or "via system"
# dir: a directory full of test files
#
# files are copied to a temporary directory, which is then deleted
use strict;
use warnings;
use 5.010;
use File::Temp qw( tempdir );
use File::Copy;
use Time::HiRes qw( gettimeofday tv_interval );
my $method = shift or die usage();
$method = lc $method;
die "Invalid method '$method'\n" unless $method eq 'p' || $method eq 's';
my $source = shift or die usage();
die "'$source' is not a directory\n" unless -d $source;
$source =~ s|/$||g;
my $target = tempdir( CLEANUP => 1 );
opendir my $DIR, $source or die "Couldn't open '$source':$!";
my $count = 0;
my $start = [ gettimeofday() ];
while( my $file = readdir $DIR ){
my $fullname = "$source/$file";
next if $file eq '.' || $file eq '..' || -d $fullname;
if ( $method eq 'p' ){
copy $fullname, $target;
}
else {
system( 'cp', $fullname, $target );
}
$count++;
}
my $elapsed = tv_interval( $start );
print "$count files copied using " . ($method eq 'p'? 'perl':'system') .
" calls in $elapsed seconds\n";
closedir $DIR;
sub usage{
my $usage = <<ENDTEXT;
Usage:
$0 { p | s } directory
ENDTEXT
return $usage;
}
--
Henry Law n e w s @ l a w s h o u s e . o r g
Manchester, England
Henry Law n e w s @ l a w s h o u s e . o r g
Manchester, England