lcsim/src/org/lcsim/util
diff -N Timer.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ Timer.java 27 May 2009 18:06:45 -0000 1.1
@@ -0,0 +1,61 @@
+/*
+ * Timer class
+ */
+package org.lcsim.util;
+
+import org.lcsim.event.EventHeader;
+import org.lcsim.util.aida.AIDA;
+
+/**
+ * Driver to perform timing measurements on a driver. For each driver that you
+ * want to measure the timing of, instantiate a new instance of Timer with a
+ * unique name. Then add this driver both before and after the driver to be
+ * timed. An aida plot will be created showing the distribution of driver
+ * processing times.
+ *
+ * No correction is made for the time taken up in the timing measurement itself.
+ * To estimate this, one could instantiate a copy of Timer and call it twice
+ * in succession.
+ *
+ * @author Richard Partridge
+ */
+public class Timer extends Driver {
+
+ private String _message;
+ private int _last_hash;
+ private long _start = 0;
+ private AIDA _aida;
+
+ /**
+ * Timer constructor. The message string is passed to aida as the title
+ * for a cloud, so it should be unique for each timing measurement.
+ *
+ * @param message title for aida cloud plot
+ */
+ public Timer(String message) {
+ _message = message;
+ _aida = AIDA.defaultInstance();
+ }
+
+ /**
+ * Timer driver process method. If this is the first call for a given
+ * event, it saves the start time. For subsequent calls, it plots the
+ * time difference between the current time and the saved start time.
+ *
+ * @param event event header
+ */
+ @Override
+ public void process(EventHeader event) {
+ super.process(event);
+ int hash = event.hashCode();
+ long time = System.currentTimeMillis();
+ if (hash == _last_hash) {
+ double duration = (time - _start) / 1000.;
+ _aida.cloud1D(_message).fill(duration);
+ } else {
+ _start = time;
+ }
+ _last_hash = hash;
+ }
+
+}