我可以在Perl中使用以下两种方式:
my $str = "hello";
my $o1 = $str." world";
my $o2 = "$str world";
Run Code Online (Sandbox Code Playgroud)
他们做同样的事情,虽然我想知道他们在谈论表现时是否相同.我怎么检查呢?有没有一个特殊的在线工具?
基准测试是实现这一目标的方法.
您描述的两种方法称为连接和插值,因此我们将在测试中将这些名称用作标签.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
my $str = 'hello';
# Run this two subroutines for at least 3 CPU seconds.
# That "negative seconds" parameter is possibly the worst
# interface in all of Perl!
timethese(-3, {
concat => sub { $str . ' world' },
interp => sub { "$str world" },
});
Run Code Online (Sandbox Code Playgroud)
结果......
$ perl benchmark
Benchmark: running concat, interp for at least 3 CPU seconds...
concat: 4 wallclock secs ( 3.16 usr + 0.00 sys = 3.16 CPU) @ 32680513.61/s (n=103270423)
interp: 3 wallclock secs ( 3.22 usr + 0.00 sys = 3.22 CPU) @ 29918605.90/s (n=96337911)
Run Code Online (Sandbox Code Playgroud)
连接稍微快一点[ 注意:正如评论中指出的那样,注意事实确实如此 - 任何差异都在这些测试的误差范围内].但请注意,为了找到这个微小的差异,我们需要运行大约一亿次测试.所以我认为你真的不必担心.