How does it compare to my solution, speed-wise?Weirdan wrote:Are you sure? In my tests table lookup was slower than Vlad's solution.Mordred wrote:But of course the "boring" solution is faster
[Challenge] IPv4 subnet mask validator
Moderator: General Moderators
Re: [Challenge] IPv4 subnet mask validator
Re: [Challenge] IPv4 subnet mask validator
I've modified it a bit to correct the "Unsupported operand types" fatal error:Apollo wrote:Bit trickery FTW:Code: Select all
function isValidIPv4Mask($mask) { $m = ~ip2long($mask); return $m && ~$m && !($m&($m+1)); }
Code: Select all
function isValidIPv4Mask2($mask)
{
if (!$m = ip2long($mask))
return false;
$m =~ $m;
return $m && ~$m && !($m&($m+1));
}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
There are 10 types of people in this world, those who understand binary and those who don't
Re: [Challenge] IPv4 subnet mask validator
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.