5 ms·
The computational time to pack and unpack an IP to an integer is vanishingly small. My old MacBook Pro does 500,000 per second of the corresponding PHP functio
by jbyers 17y ago
The computational time to pack and unpack an IP to an integer is vanishingly small. My old MacBook Pro does 500,000 per second of the corresponding PHP function.
The difference between an integer and a fifteen byte string is eleven bytes. Our database has a few hundred-million row tables (barely considered big by today's standards) that store IPs. Storing IPs as integers saves us a GB per hundred million rows in addition to a substantial index size reduction.
Your application may not need to store that much data, but it's my experience that tables with IPs are the ones that tend to get big. :)
- potatolicious 17y agoStorage is cheap - the primary win here is computation time. if(ip1 == ip2) is a lot faster as ints than strings.
- tptacek 17y agoSeeing as how the largest dotted-quad IP address fits inside rax:rdx on a modern CPU, and that two of them fit in a single cache line, I'm guessing x == y, while faster, is not "much" faster with strings than integers. I wouldn't populate an address trie with strings, but I also wouldn't give a second thought to passing them around a random C program as charstars either.
- potatolicious 17y agoMost string libs aren't that smart though - string comparisons are still byte by byte. You're now comparing something 15 times instead of 1. You can do an int32/int64 (depending on architecture) compare in a single op. The point I guess is, you can keep 'em around as charstars, but eventually you'll have to do this cast to compare them...
- tptacek 17y agoAll memcmp's are this smart. But that's kind of besides the point, right? 1 time, 14 times, if we're talking about L1 cache, we're really epsilon from pure reg/reg ALU operations, implementing effectively constant-time algorithms. I agree, int32 is faster. Like I said, it's just not "much" faster.
- sunkencity 17y agoBut what if I want to count all the 10.x requests, not sure I can bitshift in an SQL query.
- cema 17y agoselect count(*) from ipTable where ip >= 167772160 and ip < 184549376 IP is four digits in a 256-base integer. You are looking for ips with the first digit 10. So, the value boundaries are: 167772160 = 256 * 256 * 256 * 10; 184549376 = 256 * 256 * 256 * 11.