I'm standardizing some libs for CPAN release and amazed at how much I switched between subs and 'use constant' for class variables
I know people are peculiar to each one, but thats not my question...
I decided to do a quick benchmark between the two and was surpised -- 'use constant' was significantly faster for use as class methods , 1/3 the time of calling a sub.
Does anyone know why? I was under the impression that use constant just creates a sub in the namespace -- so I expected it to have the same performance as using an explicitly defined subrouting. I was kind of shocked to see the difference in execution time.
The speed difference is negligble for most uses -- it took 10k calls to show the difference beyond a margin of error. but at 10 , 1k 10k, 100k and 1MM calls , the difference still is roughly 3x longer for an explicit sub than a 'use constant' defined sub.
In reply to OOP - Constant Vs. Subroutine by 2xlp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |