Re: [code-review] Benchmarking concatenation vs. interpolation
"Jody Belka" <[email protected]> Thu, 4 Dec 2003 23:28:17 -0000 (GMT)
| Newsgroups | gmane.comp.lang.perl.code-review-ladder |
|---|---|
| Message-ID | <[email protected]> |
------=_20031204232817_21123
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
Adam Turoff said:
> I have this benchmark program, and I'd like a sanity check:
> Is this a valid benchmark?
Nope, because you're declaring the variables inside benchmark subs. In the
case of the concat sub this doesn't have a major effect on things
(although, it /does/ have an effect, as we'll see later). However, with
the interp sub perl realises that you assign to the variable and then
immediately discard it, so it can optimise it away. If you define the
variables outside of the benchmark subs, then perl will leave things alone
and you will get a proper result.
(Also add abigails reasons)
I've created a modified version of your benchmark script (please find
attached), that carries out the following runs:
interp and concat
as you wrote them (_adam)
assigning to already existing variables (_knew)
as abigail wrote them (_abigail)
as abigail but defining the variables inside the subs (_abi_adam)
And here are the results (first % column only to reduce width):
Rate interp_knew
interp_knew 267724/s --
concat_adam 274652/s 3%
concat_knew 281098/s 5%
interp_abigail 288919/s 8%
interp_adam 438631/s 64%
concat_abi_adam 458605/s 71%
interp_abi_adam 461936/s 73%
concat_abigail 480052/s 79%
So, the fastest method is apparently to use concatenation,but within one
assignment statement.
--
Jody
knew (at) pimb (dot) org
------=_20031204232817_21123
Content-Type: application/octet-stream; name="benchmark_test"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="benchmark_test"
IyEvdXNyL2Jpbi9wZXJsIC13Cgp1c2Ugc3RyaWN0Owp1c2UgQmVuY2htYXJrIHF3KDphbGwpOwoK
b3VyICRib3JkZXIgPSAwOwpvdXIgJGNlbGxzcGFjaW5nID0gMDsKb3VyICRjZWxscGFkZGluZyA9
IDA7CgpteSAkdGFibGUzOwpteSAkdGFibGU0OwpteSAkdGFibGU1OwpteSAkdGFibGU2OwoKY21w
dGhlc2UgKC01LCB7CiAgIGludGVycF9hZGFtID0+IHN1YiB7CiAgICAgIG15ICR0YWJs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lcnBfYWJpZ2FpbCA9PiBzdWIgewogICAgICAkdGFibGU1ID0K
ICAgICAgICAgcXEgKDx0YWJsZSBib3JkZXI9IiRib3JkZXIiIGNlbGxzcGFjaW5nPSIkY2VsbHNw
YWNpbmciIGNlbGxwYWRkaW5nPSIkY2VsbHBhZGRpbmciPiA8L3RhYmxlPik7CiAgIH0sICAgIAog
ICBjb25jYXRfYWJpZ2FpbCA9PiBzdWIgewogICAgICAkdGFibGU2ICA9ICc8dGFibGUgYm9yZGVy
PSInIC4gJGJvcmRlciAuICciIGNlbGxzcGFjaW5nPSInIC4KICAgICAgICAgICAgICAgICAkY2Vs
bHNwYWNpbmcgLiAnIiBjZWxscGFkZGluZz0iJyAuICRjZWxscGFkZGluZyAuCiAgICAgICAgICAg
ICAgICAgJyI+IDwvdGFibGU+JzsKICAgfSwKICAgaW50ZXJwX2FiaV9hZGFtID0+IHN1YiB7CiAg
ICAgIG15ICR0YWJsZTcgPQogICAgICAgICBxcSAoPHRhYmxlIGJvcmRlcj0iJGJvcmRlciIgY2Vs
bHNwYWNpbmc9IiRjZWxsc3BhY2luZyIgY2VsbHBhZGRpbmc9IiRjZWxscGFkZGluZyI+IDwvdGFi
bGU+KTsKICAgfSwgICAgCiAgIGNvbmNhdF9hYmlfYWRhbSA9PiBzdWIgewogICAgICBteSAkdGFi
bGU4ICA9ICc8dGFibGUgYm9yZGVyPSInIC4gJGJvcmRlciAuICciIGNlbGxzcGFjaW5nPSInIC4K
ICAgICAgICAgICAgICAgICAkY2VsbHNwYWNpbmcgLiAnIiBjZWxscGFkZGluZz0iJyAuICRjZWxs
cGFkZGluZyAuCiAgICAgICAgICAgICAgICAgJyI+IDwvdGFibGU+JzsKICAgfSwKfSk7CgpkaWUg
IlVuZXF1YWwiIHVubGVzcyAkdGFibGU1IGVxICR0YWJsZTY7Cg==
------=_20031204232817_21123--