Geobits has already mentioned the naive approach and if you are dealing with about 100 time stamps, that seems reasonably fast. I wouldn't bother putting the stamps into one-minute buckets first. Calculate the rate activities / time_span
and find the longest stretch based on number of activities or time span where the rate exceeds 1/60. (Or just find out whether such a time span exists.)
In your example data (those in the code example), activities happen in chunks of at most 20 minutes over a period of a week. These chunks have large gaps between them. You can use the nature of the data to improve the naive algorithm by looking at your time stamps chunk-wise. This has the benefit that you can rule out sequences with fewer than 10 timestamps right away. Also, the naiveté of the approach is less dominant, because your inner loop has to do only n - 10
iterations where n
rarely exceeds 20.
Here's the approach in pseudocode. (I'm not familiar with PHP.)
flag_piecewise(time[])
gap = 60 * 60 # choose approriate minimum gap, e.g. 1h
start = 0
curr = 1
while true
if (curr == len(time) || time[curr] - time[curr - 1] > gap)
if (flag_range(time[start:curr]) return true
if (curr == len(time)) break
start = curr
curr++
return false
Here flag_range
is the implementation of the naive approach. In a quick test on your sample data, I got a speed-up of about 20. (The naive approach was fast enough, I think, but you get the speed-up without adding much complexity.)