Google guava EventBus の通知で消費する時間を計測してみる。
// POST通知されるクラス
import java.text.NumberFormat;
import com.google.common.eventbus.Subscribe;
public class Foo implements Runnable{
private NumberFormat nformat;
public Foo(){
nformat = NumberFormat.getInstance();
nformat.setMaximumFractionDigits(3);
nformat.setMinimumFractionDigits(3);
}
@Override
public void run(){
}
@Subscribe
public void setTime(Long time){
long ontime = System.nanoTime();
double d = (ontime - time) / 1000000.000;
System.out.println(nformat.format(d)+" msec");
}
}
// POSTの実行
ExecutorService service = Executors.newSingleThreadExecutor();
Foo foo = new Foo();
EventBus bus = new EventBus();
bus.register(foo);
service.submit(foo);
for(int i=0;i < 10;i++){
bus.post(System.nanoTime());
}
service.shutdown();
これを遅いPC、Intel Pentium 1.8 GHz で実行した結果は、
以下のとおり、最初だけ時間が掛かってる。
15.341 msec
0.108 msec
0.058 msec
0.058 msec
0.074 msec
0.058 msec
0.057 msec
0.058 msec
0.061 msec
0.058 msec