Please check out the comments to see more information about this topic.
Going to put this out there because I saw some code the other day that printed comma separated strings and scalars, and it got me curious about how expensive it was to print a list of items, rather than concatenating them into a string and passing that to print.
I’ll let the code speak for itself:
my $x = 'test';
my $r = timethese(
10000000,
{
'commaprint' => sub { print STDERR 'test', 'test', 'test', 'test', 'test', 'test'; },
'varcommaprint' => sub { print STDERR $x, $x, $x, $x, $x, $x; },
'interpprint' => sub { print STDERR "$x $x $x $x $x $x"; },
'concatprint' => sub { print STDERR 'test' . 'test' . 'test' . 'test' . 'test' . 'test'; },
'singleprint' => sub { print STDERR 'test'; },
} );
And the result on my laptop:
commaprint: 33 wallclock secs (12.46 usr + 19.77 sys = 32.23 CPU) @ 310269.93/s (n=10000000)
varcommaprint: 32 wallclock secs (12.76 usr + 19.74 sys = 32.50 CPU) @ 307692.31/s (n=10000000)
interpprint: 10 wallclock secs ( 7.44 usr + 3.68 sys = 11.12 CPU) @ 899280.58/s (n=10000000)
concatprint: 6 wallclock secs ( 3.02 usr + 3.58 sys = 6.60 CPU) @ 1515151.52/s (n=10000000)
singleprint: 6 wallclock secs ( 2.97 usr + 3.52 sys = 6.49 CPU) @ 1540832.05/s (n=10000000)
I’m not completely surprised by the result, but I would’t want to speculate about what’s going on under the covers without spending time studying the perl code. For now this will just have to leave this out there as something to consider in the future.