http://www.perlmonks.org?node_id=1214907


in reply to '%hash = ()' is slower than 'undef %hash'

Thank you very much for discussion and benchmarks!

I was simply upsolving a competitive programming problem which had 1000 ms time limit and big tests with undirected graphs (tries). After my solution with %hash = () got a verdict time-limit-exceeded, I tried to find how to optimise it. And it was surprising for me, when I found that my program is running ~20% faster when I change to undef %hash, got verdict accepted in 850 ms on max tests. Both variations didn't influence a correctness of an algorithm, only running time. I used %hash as global (here it didn't influence solving time comparing with declaring with 'my').
I'm happy that in newer versions optimisation took place, as dave_the_m have mension. I thought maybe it is unknown or unoptimized thing.
edited

Replies are listed 'Best First'.
Re^2: '%hash = ()' is slower than 'undef %hash'
by rsFalse (Chaplain) on May 19, 2018 at 12:29 UTC
    I haven't done benchmark by myself, only compared running times of tests cases on CP platform. Problem. Submitions: >1000 ms, 800 ms (maybe not readable, but here only 16th line differs). Perl 5.20.1.
Re^2: '%hash = ()' is slower than 'undef %hash'
by Anonymous Monk on May 19, 2018 at 12:10 UTC
    Probably determined by whether-or-not garbage collection had been triggered.
      Perl does reference counting, it's always triggered, sundial