#!/usr/bin/perl use strict; use Benchmark; sub line_by_line { open (IN, "/usr/share/dict/words"); while (){ if ($_ =~ /hello world/){ # do what ever } } close IN; } sub block { open (IN, "/usr/share/dict/words"); my @lines = ; close (IN); foreach (@lines){ if ($_ =~ /hello world/){ # do what ever } } } timethese(100,{ 'line' => \&line_by_line, 'block' => \&block });