Reading binary data from a file
You could use numpy.digitize to group the data and vectorize the loop. Demo:

&gt;&gt;&gt; clicks
array([(0L, 7), (1L, 0), (2L, 0), (3L, 0), (4L, 7), (5L, 0), (6L, 0),
       (7L, 0), (8L, 0), (9L, 7)],
      dtype=[('time', '&lt;u8'), ('channel', '&lt;u2')])
&gt;&gt;&gt; bins = clicks['time'][clicks['channel']==7]
&gt;&gt;&gt; bins
array([0, 4, 9], dtype=uint64)
&gt;&gt;&gt; ind = np.digitize(clicks['time'], bins) - 1
&gt;&gt;&gt; ind
array([0, 0, 0, 0, 1, 1, 1, 1, 1, 2])
&gt;&gt;&gt; bins[ind]
array([0, 0, 0, 0, 4, 4, 4, 4, 4, 9], dtype=uint64)
&gt;&gt;&gt; clicks['time'] - bins[ind]
array([0, 1, 2, 3, 0, 1, 2, 3, 4, 0], dtype=uint64)