It is quite likely that my results will not match those obtained on different computers, versions of Perl, operating systems or other application mixes, but I can say that all results were run multiple times and then averaged.
1. Which of the following snippets averages the most executions per second?
a) 's/\s*#.*//' b) '$_ = $` if /#/' c) '($_) = split/#/' d) '$_ = substr($_, 0, $-[0]) if /#/' e) 'if ((my $p = index($_, "#")) > -1) { substr($_, $p, -1, "") }'
Answer: d |
2. Which snippet from the previous question averages the fewest executions per second?
Answer: c |
3. Which of the following snippets can cause a runtime error?
a) '{}' b) '' c) ';' d) All of the above e) None of the above
|
4. What runtime error is most likely produced by the previous question?
|
5. Which of the following snippets averages the most executions per second?
a) '$_ = (split/#/)[0]' b) '($_) = split/#/' c) Too close to tell
Answer: b, by about 9% |
6. Which of the following snippets averages the most executions per second?
a) '$i = ""; $i = 1 if ($_ % 2) == 0' b) 'if (($_ % 2) == 0) {$i = 1} else {$i = ""}' c) Too close to tell
Answer: b, by about 4% |
7. Which of the following snippets averages the most executions per second?
a) '($str1, $str2) = split/:/ if /:/', b) 'if (/:/) {$str1 = $`; $str2 = $\'}', c) 'if (/:/) {$str1 = substr($_,0, $-[0]); $str2 = substr($_,$+[0]) +}' d) '($str1,$str2) = m/(\d*):(\d*)/ if /:/' e) Too close to call.
Answer: e, allowing for at least 3% error. |
8. Which of the snippets in the previous question runs the fastest in the following code?
$_ = "123:45678"; foreach (1..2000000) { # snippet goes here ; }
Answer: e, allowing for at least 3% error. |
a) 'foreach (1..$n){$m = $_}' b) '$m = $_ for (1..$n)' c) 'for ($i=1;$i<$n+1;$i++) {$m = $i}' d) Too close to call.
Answer: c, by about 10% |
my ($i, $m); my $n = 5000000; # snippet goes here
Answer: b, by about 7% |
--Jim
In reply to Benchmarking Quiz by jlongino
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |