Skip to content

Instantly share code, notes, and snippets.

Created November 9, 2012 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4045415 to your computer and use it in GitHub Desktop.
Save anonymous/4045415 to your computer and use it in GitHub Desktop.
NanosTest.java
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
public final class NanosTest {
public static void main(final String ... args) throws Exception {
final int N = 50;
final int M = 10000;
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(N);
final ConcurrentMap<String, long[]> timesMap = new ConcurrentHashMap<String, long[]>(N);
for(int i = 0; i < N; i++){
final Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
final String name = Thread.currentThread().getName();
latch1.await();
final long[] times = new long[M];
for(int j = 0; j < M; j++){
times[j] = System.nanoTime();
}
timesMap.put(name, times);
latch2.countDown();
} catch (InterruptedException e) {
}
}
});
thread.setName("T" + i);
thread.start();
}
latch1.countDown();
latch2.await();
String prevName = null;
long[] prev = null;
for(int i = 0; i < N; i++){
final String name = "T" + i;
final long[] times = timesMap.get(name);
if (prev != null){
long diff = 0;
for(int j = 0; j < M; j++){
diff += times[j] - prev[j];
}
diff /= M;
System.out.println(name + " - " + prevName + " : " + (diff / 1000) / 1e3 + " ms");
}
prevName = name;
prev = times;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment