Which is faster for resolving an IP?

136 Views Asked by At

Has anyone done any testing to see which is faster/more efficient/better for resolving an IP address in a PHP script?

This

exec('host '. $_SERVER['REMOTE_ADDR']);

or this

gethostbyaddr($_SERVER['REMOTE_ADDR']);
2

There are 2 best solutions below

0
On

If there is a native function, then you should use it instead of using an external command.

Using a external command make your program OS dependent.

3
On

You should try it yourself but I would think the cost of starting a whole new process would be more than just calling the gethostbyaddr function. Going the external executable route also makes you dependent on a lot of other things, like the OS, your path being set up correctly, the possibility that the output of host may change, and so on.

Optimisation mantra number 1 is measure, don't guess! Number 2 is only optimise if you have established it's a bottleneck, so make sure it's actually causing a problem before you waste time trying to fix something irrelevant.