title: Calculating number of messages per second in a rolling window?
body: <p>I have messages coming into my program with millisecond resolution (anywhere from zero to a couple hundred messages a millisecond).</p>
  
  <p>I'd like to do some analysis. Specifically, I want to maintain multiple rolling windows of the message counts, updated as messages come in. For example,</p>
  
  <ul>
  <li># of messages in last second</li>
  <li># of messages in last minute</li>
  <li># of messages in last half-hour <em>divided by</em> # of messages in last hour</li>
  </ul>
  
  <p>I can't just maintain a simple count like <em>"1,017 messages in last second"</em>, since I won't know when a message is older than 1 second and therefore should no longer be in the count...</p>
  
  <p>I thought of maintaining a queue of all the messages, searching for the youngest message that's older than one second, and inferring the count from the index. However, this seems like it would be too slow, and would eat up a lot of memory.</p>
  
  <p>What can I do to keep track of these counts in my program so that I can efficiently get these values in real-time?</p>