use Benchmark qw( cmpthese ); cmpthese(-3, { 'for' => sub { open(my $fh_in, '<', $0) or die("Unable to open input file: $!\n"); my $infile_num_chunks = int((-s $fh_in) / 8); for ($chunk_counter = 0; $chunk_counter < $infile_num_chunks +1; ++$chunk_counter) { read($fh_in, my $data, 8); # ... } # ... 1; }, 'while' => sub { open(my $fh_in, '<', $0) or die("Unable to open input file: $!\n"); while (read($fh_in, my $data, 8) == 8) { # ... } # ... 1; }, });