#!/usr/bin/perl -w use Benchmark qw(:all) ; use strict; my $lsofcmd1 = "/usr/local/bin/lsof -c file_a|wc -l"; my $lsofcmd2 = "/usr/local/bin/lsof|grep file_a|wc -l"; my $lsofcmd3 = "/usr/local/bin/lsof"; my $count = "500"; my $results = timethese($count, { 'lsof_c' => sub { system($lsofcmd1); }, 'lsof_grep' => sub { system($lsofcmd2); }, 'lsof_perl' => sub { my @ret = `$lsofcmd3`; print scalar(my @openfiles = grep(/file_a/,@ret)); }, }, 'none' ); cmpthese( $results ) ; ##### Results #### # Rate lsof_perl lsof_grep lsof_c # lsof_perl 13.4/s -- -96% -96% # lsof_grep 321/s 2293% -- -5% # lsof_c 338/s 2422% 5% --