Premium Member Database last update: Wednesday, September 28, 2016 6:02:44 GMT-0700

Which Runs Faster INET_ATON or IP2LONG

Working with IP addresses is easier and more efficient if you can convert IP addresses to decimal. In decimal form IP addresses take up less space in your database and are much easier to search within a network range than a four octet address. You can easily convert IP and decimal addresses using MySQL’s INET_ATON and INET_NTOA or PHP’s ip2long and long2ip functions. But which is faster?

At Country IP Blocks we deal with billions of IP addresses thousands of times each day. So deciding which function to use and when is important to us. While the difference might be slight, the right choice could provide a significant performance boost when measured over time.

Function Definitions

INET_ATON()

Given the dotted-quad representation of an IPv4 network address as a string, inet_aton() returns an integer that represents the numeric value of the address in network byte order (big endian).

INET_NTOA()

Given a numeric IPv4 network address in network byte order, inet_ntoa returns the dotted-quad representation of the address as a string.

IP2LONG()

The function ip2long() generates an IPv4 Internet network address from its Internet standard format (dotted string) representation.

LONG2IP()

The function long2ip() generates an Internet address in dotted format (i.e.: aaa.bbb.ccc.ddd) from the proper address representation.

Our Test

We used arrays of 10,000 IP addresses in both IP octet and IP decimal formats. We placed a timer at the beginning and the end of our process to determine the running time of each test. As we needed a connection to a mysql server in order to use INET_NTOA and INET_ATON we created the mysql connection in advance of the script start time. We felt this would most accurately represent a comparison between MySQL and PHP.

We repeated each test ten times and averaged the resul.

Our first comparison converted IP addresses to numeric using inet_aton() and ip2long(). We used the 10,000 IP addresses from within this array: IP Array 10,000.

IP to Numeric Scripts

MySQL INET_ATON

foreach ($ip_address as $value) {


		$query="SELECT inet_aton('$value')";
		
	
	$results = mysql_query($query);
	while(list ($value) = mysql_fetch_row($results)) {


		$time_end = microtime_float();
		$time = $time_end - $time_start;
	}
	
	}

PHP IP2LONG

foreach ($ip_address as $value) {
    
	$ip = ip2long($value);
	
	$time_end = microtime_float();
		$time = $time_end - $time_start;
	}

IP to Numeric Results

Times: inet_aton

0.348609924316
0.300888776779 
0.458437919617 
0.289735078812 
0.544997930527
0.371957063675
0.515553951263 
0.525588989258
0.528133869171

Total = 3.883903503418
Average = 0.3883903503418

Times: ip2long

0.0224158763885 
0.0221440792084 
0.0231518745422 
0.029060125351 
0.0288360118866 
0.0247418880463 
0.0251941680908 
0.0229442119598
0.0238349437714 
0.0217750072479

Total = 0.2440981864929
Average = 0.02440981864929

Our second comparison converted numeric addresses to IP using inet_ntoa() and long2ip(). We used the 10,000 numeric addresses within this array: IP Decimal Array 10,000.

Numeric to IP Scripts

MySQL INET_NTOA

foreach ($ip_address as $value) {

		$query="SELECT inet_ntoa('$value')";
	
	$results = mysql_query($query);
	while(list ($value) = mysql_fetch_row($results)) {
		
		$time_end = microtime_float();
		$time = $time_end - $time_start;
	}
	
	}

PHP LONG2IP

foreach ($ip_address as $value) {
 
	$ip = long2ip($value);

	$time_end = microtime_float();
		$time = $time_end - $time_start;
	}

Times: inet_ntoa

0.323868989944
0.452445030212
0.341184854507
0.491384029388
0.52533197403
0.424556970596
0.493114948273
0.538600921631
0.32187795639
0.292950868607

Total = 4.205316543578
Average = 0.4205316543578

Times: long2ip

0.035698890686
0.0324988365173
0.0302648544312
0.0297911167145
0.0271110534668
0.0311720371246
0.0268168449402
0.0275690555573
0.0304639339447
0.0333690643311

Total = 0.3047556877137
Average = 0.03047556877137

Using our parameters the PHP functions performed significantly faster than their MySQL counterparts.

When deciding on which function to use you should determine how you are going to use it and then run repeated experiments to see how each function runs over time.