10 rt26188 - benchmark speed for keys() on empty hashes
14 If you have an empty hash, the speed of keys() depends
15 on how many keys the hash previously held.
17 For global hashes, getting the count for previously
18 big hashes was substantially slower than for lexical hashes.
20 This test checks that the speed difference for getting
21 the number or list of keys from an empty hash is about the same
22 (< 25%) for lexical and global hashes, both previously big and small.
26 This test tests against RT ticket #26188
28 L<http://rt.perl.org/rt3/Public/Bug/Display.html?id=26188>
32 use vars qw(%h_big %h_small);
33 my %l_big = (1..50000);
34 my %l_small = (1..10);
39 delete @h_big{keys %h_big};
40 delete @h_small{keys %h_small};
41 delete @l_big{keys %l_big};
42 delete @l_small{keys %l_small};
44 my $res = timethese shift || -3, {
45 big => '1 for keys %h_big',
46 small => '1 for keys %h_small',
47 scalar_big => '$a = keys %h_big',
48 scalar_small => '$a = keys %h_small',
50 lex_big => '1 for keys %l_big',
51 lex_small => '1 for keys %l_small',
52 lex_scalar_big => '$a = keys %l_big',
53 lex_scalar_small => '$a = keys %l_small',
56 sub iters_per_second {
57 $_[0]->iters / $_[0]->cpu_p
60 sub about_as_fast_ok {
61 my ($res, $key1, $key2, $name) = @_;
62 $name ||= "Speed difference between $key1 and $key2 is less than 25%";
63 my %iters_per_second = map { $_ => iters_per_second( $res->{ $_ }) } ($key1, $key2);
65 my $ratio = abs(1 - $iters_per_second{ $key1 } / ($iters_per_second{ $key2 } || 1 ));
66 if (! cmp_ok( $ratio, '<', 0.25, $name )) {
67 diag( sprintf "%20s: %12.2f/s\n", $key1, $iters_per_second{ $key1 } );
68 diag( sprintf "%20s: %12.2f/s\n", $key2, $iters_per_second{ $key2 } );
72 about_as_fast_ok( $res, 'scalar_big', 'scalar_small',"Checking the count of hash keys in an empty hash (global)");
74 about_as_fast_ok( $res, 'big', 'small', "Checking the list of hash keys in an empty hash (global)");
76 about_as_fast_ok( $res, 'lex_scalar_big', 'lex_scalar_small',"Checking the count of hash keys in an empty hash (lexical)");
78 about_as_fast_ok( $res, 'lex_big', 'lex_small', "Checking the list of hash keys in an empty hash (lexical)");
80 about_as_fast_ok( $res, 'lex_scalar_big', 'scalar_big',"Checking the count of hash keys in an empty hash, global vs. lexical");
82 about_as_fast_ok( $res, 'lex_big', 'big', "Checking the list of hash keys in an empty hash, global vs. lexical");