Skip to content

Instantly share code, notes, and snippets.

@payam-int
Created December 4, 2017 18:49
Show Gist options
  • Save payam-int/13c5c9670ed26f436a3348110d54dad8 to your computer and use it in GitHub Desktop.
Save payam-int/13c5c9670ed26f436a3348110d54dad8 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
/**
* @Author Payam Mohammadi (payam.int@gmail.com)
*/
public class SetSocketTimeoutPerformance {
public static void main(String[] args) throws IOException, InterruptedException {
String host = "localhost";
int port = 8585;
Socket socket = new Socket(host, port);
long time = 0, t1, t2;
for (int i = 0; i < 1000000; i++) {
t1 = System.nanoTime();
socket.setSoTimeout((int) (Math.random()*1000 + 500));
t2 = System.nanoTime();
time += t2-t1;
}
System.out.println(((double) (time / 1000000))/1000);
// 0.162s @ localhost
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment