in reply to Re: From Array 2 string
in thread From Array 2 string

use Benchmark qw( cmpthese ); # From "man perlrun": my @array = qw( Perl is a language optimized for scanning arbitrary text files, extracting information from those text files, and printing reports based on that information. It's also a good language for many system management tasks. The language is intended to be practical (easy to use, efficient, complete) rather than beautiful (tiny, elegant, minimal). ); my %tests = ( 'join' => sub { my $str = join('', @array); +1; } , 'stringify' => sub { local $" = ''; my $str = "@array"; +1; } ); print(@array . " words:\n\n"); cmpthese(-2, \%tests); print("\n\n"); splice(@array, 20); print(@array . " words:\n\n"); cmpthese(-2, \%tests); print("\n\n"); @array = (@array) x 100; print(@array . " words:\n\n"); cmpthese(-2, \%tests);
20 words: Rate stringify join stringify 155272/s -- -29% join 219556/s 41% -- 51 words: Rate stringify join stringify 83452/s -- -16% join 99407/s 19% -- 2000 words: Rate stringify join stringify 2736/s -- -3% join 2813/s 3% --

They're about the same speed, but stringification has a much bigger overhead. Better off with join.

Replies are listed 'Best First'.
Re^3: From Array 2 string
by Aristotle (Chancellor) on Nov 15, 2005 at 19:23 UTC

    Errm, what? At a rate of 155272/s, does it even matter for 99.99% of the apps out there? They’re going to be waiting for the harddrive, or the network, or the terminal, or whatever.

    Better off with whichever one is more readable.

    (Which is going to be join most of the time – but recommending it on the basis of performance is plainly silly.)

    Makeshifts last the longest.