Arya
Arya

Reputation: 8995

InetAddress.getByName very slow for some hosts, but not others

I have noticed that InetAddress is very slow for some hosts.

Below is the relevant code

final long startTime = System.currentTimeMillis();

try {
    System.out.println("start");
    InetAddress address = InetAddress.getByName("smartface.com");
    System.out.println(address);
} catch (UnknownHostException e) {
    e.printStackTrace();
}

final long endTime = System.currentTimeMillis();
System.out.println("Total execution time: " + (endTime - startTime));

For the host "smartface.com" it takes about 20000 miliseconds resulting in a "UnknownHostException"

start
java.net.UnknownHostException: smartface.com: Name or service not known
...
...
Total execution time: 20024
end

But when running the same code for host "smartfacesmart.com" it takes about 280 milliseconds.

start
java.net.UnknownHostException: smartfacesmart.com: Name or service not known
...
...
Total execution time: 284
end

I don't understand why there is such a big difference between the two hosts. Both result in the same exception.

Is there anything I can to make the process faster for the slower hosts?

Upvotes: 0

Views: 293

Answers (0)

Related Questions