use strict; use Benchmark qw(cmpthese); my @x; $x[0]= "mark " x 35; $x[1]= "asdfasdfasdfasdfasdf " x 35; $x[2]= "as " x 31; $x[3]= "asdfasdfasdfasdfasdfasdf " x 100; $x[4]= "asdfasdfasdfasdfasdfasdf " x 10; # http://www.cpan.org/doc/manual/html/pod/perlfunc/index.html # # Well, that was no help... fricking http conventions # cmpthese (1000000, { 'regexp' => ' foreach (@x) { /^((?:\S+\s*){1,30})/; print $1; } ', 'split_join' => ' foreach (@x) { print join ( " ", (split " ", $_,31)[0..29] ); } ', 'for_index_substr' => ' foreach (@x) { my $ind = index ($_, " "); for (0..29) { last if $ind == -1; $ind = index $_, " ", $ind; } print substr($_,0,$ind); } ', } );