Skip Menu |

This queue is for tickets about the SHARYANTO-Utils CPAN distribution.

Report information
The Basics
Id: 94807
Status: resolved
Priority: 0/
Queue: SHARYANTO-Utils

People
Owner: Nobody in particular
Requestors: perl [...] toby.ink
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Potential for optimizing looks_like_int()
Currently the check in looks_like_int() looks like this: $l==1 || $l==2 || $l==9 || $l==10 || $l==4352 However, the most common return values from looks_like_number($int) seem to be: looks_like_number(1); # 4352 looks_like_number(-1); # 4352 looks_like_number("1"); # 1 looks_like_number("-1"); # 9 Getting 2 or 10 to appear is a challenge. Swapping the tests around to put the most likely values up front makes the sub about 20% faster overall: $l==4352 || $l==1 || $l==9 || $l==2 || $l==10 That said, this is even faster: sub looks_like_int { $_[0] =~ /\A-?[0-9]+\z/; }
On Wed Apr 16 16:20:18 2014, TOBYINK wrote: Show quoted text
> Currently the check in looks_like_int() looks like this: > > $l==1 || $l==2 || $l==9 || $l==10 || $l==4352 > > However, the most common return values from looks_like_number($int) > seem to be: > > looks_like_number(1); # 4352 > looks_like_number(-1); # 4352 > looks_like_number("1"); # 1 > looks_like_number("-1"); # 9 > > Getting 2 or 10 to appear is a challenge. Swapping the tests around to > put the most likely values up front makes the sub about 20% faster > overall: > > $l==4352 || $l==1 || $l==9 || $l==2 || $l==10 > > That said, this is even faster: > > sub looks_like_int { > $_[0] =~ /\A-?[0-9]+\z/; > }
Yeah, 2 and 10 don't seem to appear anymore. The constants probably changed in recent perls (They are meant to be internal anyway, but accidentally exposed by looks_like_number()). Thanks for this, but in light of #94805 I'm deprecating this module. Luckily I haven't used this anywhere (but I used the same trick in Data::Sah though, might need to tweak that). Regards, Steven
Module is removed for now. Closing ticket. On Wed Apr 16 21:46:30 2014, SHARYANTO wrote: Show quoted text
> On Wed Apr 16 16:20:18 2014, TOBYINK wrote:
> > Currently the check in looks_like_int() looks like this: > > > > $l==1 || $l==2 || $l==9 || $l==10 || $l==4352 > > > > However, the most common return values from looks_like_number($int) > > seem to be: > > > > looks_like_number(1); # 4352 > > looks_like_number(-1); # 4352 > > looks_like_number("1"); # 1 > > looks_like_number("-1"); # 9 > > > > Getting 2 or 10 to appear is a challenge. Swapping the tests around > > to > > put the most likely values up front makes the sub about 20% faster > > overall: > > > > $l==4352 || $l==1 || $l==9 || $l==2 || $l==10 > > > > That said, this is even faster: > > > > sub looks_like_int { > > $_[0] =~ /\A-?[0-9]+\z/; > > }
> > Yeah, 2 and 10 don't seem to appear anymore. The constants probably > changed in recent perls (They are meant to be internal anyway, but > accidentally exposed by looks_like_number()). Thanks for this, but in > light of #94805 I'm deprecating this module. Luckily I haven't used > this anywhere (but I used the same trick in Data::Sah though, might > need to tweak that). > > Regards, > Steven