This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for fb10a8a78bba
[perl5.git] / t / perf / benchmarks
CommitLineData
24fb648d
DM
1#!perl
2
9e7973fa 3# This file specifies an array-of-hashes that define snippets of code that
24fb648d
DM
4# can be run by various measurement and profiling tools.
5#
6# The basic idea is that any time you add an optimisation that is intended
7# to make a particular construct faster, then you should add that construct
8# to this file.
9#
10# Under the normal test suite, the test file benchmarks.t does a basic
11# compile and run of each of these snippets; not to test performance,
12# but just to ensure that the code doesn't have errors.
13#
14# Over time, it is intended that various measurement and profiling tools
15# will be written that can run selected (or all) snippets in various
16# environments. These will not be run as part of a normal test suite run.
17#
9e7973fa
DM
18# It is intended that the tests in this file will be lightweight; e.g.
19# a hash access, an empty function call, or a single regex match etc.
20#
24fb648d
DM
21# This file is designed to be read in by 'do' (and in such a way that
22# multiple versions of this file from different releases can be read in
23# by a single process).
24#
9e7973fa
DM
25# The top-level array has name/hash pairs (we use an array rather than a
26# hash so that duplicate keys can be spotted) Each name is a token that
27# describes a particular test. Code will be compiled in the package named
28# after the token, so it should match /^(\w|::)+$/a. It is intended that
29# this can be used on the command line of tools to select particular
30# tests.
31# In addition, the package names are arranged into an informal hierarchy
32# whose top members are (this is subject to change):
33#
34# call:: subroutine and method handling
35# expr:: expressions: e.g. $x=1, $foo{bar}[0]
36# loop:: structural code like for, while(), etc
37# regex:: regular expressions
38# string:: string handling
24fb648d 39#
9e7973fa
DM
40#
41# Each hash has three fields:
24fb648d
DM
42#
43# desc is a description of the test
44# setup is a string containing setup code
45# code is a string containing the code to run in a loop
46#
47# So typically a benchmark tool might do something like
48#
49# eval "package $token; $setup; for (1..1000000) { $code }"
9e7973fa
DM
50#
51# Currently the only tool that uses this file is Porting/bench.pl;
52# try C<perl Porting/bench.pl --help> for more info
24fb648d
DM
53
54
9e7973fa
DM
55[
56 'call::sub::3_args' => {
57 desc => 'function call with 3 local lexical vars',
58 setup => 'sub f { my ($a, $b, $c) = @_ }',
59 code => 'f(1,2,3)',
60 },
61
fedf30e1
DM
62
63 'expr::array::lex_1const_0' => {
64 desc => 'lexical $array[0]',
65 setup => 'my @a = (1)',
66 code => '$a[0]',
67 },
68 'expr::array::lex_1const_m1' => {
69 desc => 'lexical $array[-1]',
70 setup => 'my @a = (1)',
71 code => '$a[-1]',
72 },
73 'expr::array::lex_2const' => {
74 desc => 'lexical $array[const][const]',
75 setup => 'my @a = ([1,2])',
76 code => '$a[0][1]',
77 },
78 'expr::array::lex_2var' => {
79 desc => 'lexical $array[$i1][$i2]',
80 setup => 'my ($i1,$i2) = (0,1); my @a = ([1,2])',
81 code => '$a[$i1][$i2]',
82 },
83 'expr::array::ref_lex_2var' => {
84 desc => 'lexical $arrayref->[$i1][$i2]',
85 setup => 'my ($i1,$i2) = (0,1); my $r = [[1,2]]',
86 code => '$r->[$i1][$i2]',
87 },
88 'expr::array::ref_lex_3const' => {
89 desc => 'lexical $arrayref->[const][const][const]',
90 setup => 'my $r = [[[1,2]]]',
91 code => '$r->[0][0][0]',
92 },
93 'expr::array::ref_expr_lex_3const' => {
94 desc => '(lexical expr)->[const][const][const]',
95 setup => 'my $r = [[[1,2]]]',
96 code => '($r//0)->[0][0][0]',
97 },
98
99
100 'expr::array::pkg_1const_0' => {
101 desc => 'package $array[0]',
102 setup => 'our @a = (1)',
103 code => '$a[0]',
104 },
105 'expr::array::pkg_1const_m1' => {
106 desc => 'package $array[-1]',
107 setup => 'our @a = (1)',
108 code => '$a[-1]',
109 },
110 'expr::array::pkg_2const' => {
111 desc => 'package $array[const][const]',
112 setup => 'our @a = ([1,2])',
113 code => '$a[0][1]',
114 },
115 'expr::array::pkg_2var' => {
116 desc => 'package $array[$i1][$i2]',
117 setup => 'our ($i1,$i2) = (0,1); our @a = ([1,2])',
118 code => '$a[$i1][$i2]',
119 },
120 'expr::array::ref_pkg_2var' => {
121 desc => 'package $arrayref->[$i1][$i2]',
122 setup => 'our ($i1,$i2) = (0,1); our $r = [[1,2]]',
123 code => '$r->[$i1][$i2]',
124 },
125 'expr::array::ref_pkg_3const' => {
126 desc => 'package $arrayref->[const][const][const]',
127 setup => 'our $r = [[[1,2]]]',
128 code => '$r->[0][0][0]',
129 },
130 'expr::array::ref_expr_pkg_3const' => {
131 desc => '(package expr)->[const][const][const]',
132 setup => 'our $r = [[[1,2]]]',
133 code => '($r//0)->[0][0][0]',
134 },
135
136
137 'expr::arrayhash::lex_3var' => {
138 desc => 'lexical $h{$k1}[$i]{$k2}',
139 setup => 'my ($i, $k1, $k2) = (0,"foo","bar");'
140 . 'my %h = (foo => [ { bar => 1 } ])',
141 code => '$h{$k1}[$i]{$k2}',
142 },
143 'expr::arrayhash::pkg_3var' => {
144 desc => 'package $h{$k1}[$i]{$k2}',
145 setup => 'our ($i, $k1, $k2) = (0,"foo","bar");'
146 . 'our %h = (foo => [ { bar => 1 } ])',
147 code => '$h{$k1}[$i]{$k2}',
148 },
149
150
9e7973fa
DM
151 'expr::assign::scalar_lex' => {
152 desc => 'lexical $x = 1',
153 setup => 'my $x',
154 code => '$x = 1',
155 },
156 'expr::assign::2list_lex' => {
157 desc => 'lexical ($x, $y) = (1, 2)',
158 setup => 'my ($x, $y)',
159 code => '($x, $y) = (1, 2)',
24fb648d 160 },
15c41403 161
fedf30e1
DM
162
163 'expr::hash::lex_1const' => {
164 desc => 'lexical $hash{const}',
165 setup => 'my %h = ("foo" => 1)',
166 code => '$h{foo}',
167 },
168 'expr::hash::lex_2const' => {
169 desc => 'lexical $hash{const}{const}',
170 setup => 'my %h = (foo => { bar => 1 })',
171 code => '$h{foo}{bar}',
172 },
173 'expr::hash::lex_2var' => {
174 desc => 'lexical $hash{$k1}{$k2}',
175 setup => 'my ($k1,$k2) = qw(foo bar); my %h = ($k1 => { $k2 => 1 })',
176 code => '$h{$k1}{$k2}',
177 },
178 'expr::hash::ref_lex_2var' => {
179 desc => 'lexical $hashref->{$k1}{$k2}',
180 setup => 'my ($k1,$k2) = qw(foo bar); my $r = {$k1 => { $k2 => 1 }}',
181 code => '$r->{$k1}{$k2}',
182 },
183 'expr::hash::ref_lex_3const' => {
184 desc => 'lexical $hashref->{const}{const}{const}',
185 setup => 'my $r = {foo => { bar => { baz => 1 }}}',
186 code => '$r->{foo}{bar}{baz}',
187 },
188 'expr::hash::ref_expr_lex_3const' => {
189 desc => '(lexical expr)->{const}{const}{const}',
190 setup => 'my $r = {foo => { bar => { baz => 1 }}}',
191 code => '($r//0)->{foo}{bar}{baz}',
192 },
193
194
195 'expr::hash::pkg_1const' => {
196 desc => 'package $hash{const}',
197 setup => 'our %h = ("foo" => 1)',
198 code => '$h{foo}',
199 },
200 'expr::hash::pkg_2const' => {
201 desc => 'package $hash{const}{const}',
202 setup => 'our %h = (foo => { bar => 1 })',
203 code => '$h{foo}{bar}',
204 },
205 'expr::hash::pkg_2var' => {
206 desc => 'package $hash{$k1}{$k2}',
207 setup => 'our ($k1,$k2) = qw(foo bar); our %h = ($k1 => { $k2 => 1 })',
208 code => '$h{$k1}{$k2}',
209 },
210 'expr::hash::ref_pkg_2var' => {
211 desc => 'package $hashref->{$k1}{$k2}',
212 setup => 'our ($k1,$k2) = qw(foo bar); our $r = {$k1 => { $k2 => 1 }}',
213 code => '$r->{$k1}{$k2}',
214 },
215 'expr::hash::ref_pkg_3const' => {
216 desc => 'package $hashref->{const}{const}{const}',
217 setup => 'our $r = {foo => { bar => { baz => 1 }}}',
218 code => '$r->{foo}{bar}{baz}',
219 },
220 'expr::hash::ref_expr_pkg_3const' => {
221 desc => '(package expr)->{const}{const}{const}',
222 setup => 'our $r = {foo => { bar => { baz => 1 }}}',
223 code => '($r//0)->{foo}{bar}{baz}',
224 },
225
226
227 'expr::hash::exists_lex_2var' => {
228 desc => 'lexical exists $hash{$k1}{$k2}',
229 setup => 'my ($k1,$k2) = qw(foo bar); my %h = ($k1 => { $k2 => 1 });',
230 code => 'exists $h{$k1}{$k2}',
231 },
232 'expr::hash::delete_lex_2var' => {
233 desc => 'lexical delete $hash{$k1}{$k2}',
234 setup => 'my ($k1,$k2) = qw(foo bar); my %h = ($k1 => { $k2 => 1 });',
235 code => 'delete $h{$k1}{$k2}',
236 },
237
238
41f678d9 239 'expr::index::utf8_position_1' => {
15c41403
JR
240 desc => 'index of a utf8 string, matching at position 1',
241 setup => 'utf8::upgrade my $x = "abc"',
242 code => 'index $x, "b"',
243 },
24fb648d 244
fedf30e1 245];