Page 3 of 3
Re: [Challenge] IPv4 subnet mask validator
Posted: Wed Apr 28, 2010 7:44 am
by Apollo
Weirdan wrote:Mordred wrote:But of course the "boring" solution is faster
Are you sure? In my tests table lookup was slower than Vlad's solution.
How does it compare to my solution, speed-wise?
Re: [Challenge] IPv4 subnet mask validator
Posted: Fri May 28, 2010 1:31 pm
by VladSun
Apollo wrote:Bit trickery FTW:
Code: Select all
function isValidIPv4Mask($mask)
{
$m = ~ip2long($mask);
return $m && ~$m && !($m&($m+1));
}
I've modified it a bit to correct the "Unsupported operand types" fatal error:
Code: Select all
function isValidIPv4Mask2($mask)
{
if (!$m = ip2long($mask))
return false;
$m =~ $m;
return $m && ~$m && !($m&($m+1));
}
Speed test: 0.516259908676 seconds
The "original" Mordred's solution has the same speed results.
So it's faster then my "loop" solution. I thought the bottleneck would be the ip2long() function, but obviously it's not true
Very, very nice solutions

I think it's Mordred who deserves the prize

Re: [Challenge] IPv4 subnet mask validator
Posted: Mon May 31, 2010 4:58 am
by Mordred
Meh, I screwed up (I misread some details in the problem description). I can only take credit for the "bit trickery" idea, I was surprised noone tried this approach.