[Numpy-discussion] Managing Rolling Data

Anne Archibald peridot.faceted@gmail....
Fri Feb 23 12:26:45 CST 2007


On 23/02/07, Alexander Michael <lxander.m@gmail.com> wrote:

> I still find the ring buffer solution appealing, but I did not see a
> way to stack two arrays together without creating copies. Am I missing
> a bit of numpy cleverness?

The short answer is no; the stride in memory from one element to the
next must always be the same in an array, and so it's impossible to
join two arrays like that without copying.

There is a ring-buffer-like idea that lets you do less copying; if you
need slices of length N, you can choose M>=2N and maintain a
ringbuffer of length M instead; when you push something into the
M-N+jth position, you also push (i.e., copy) it into the jth position.
Then you can always take a contiguous slice starting anywhere from 0
to M-N-1, and you need only copy a small fraction (N/M or so) of the
values pushed on. You never move values in the ringbuffer, and there
are no big spikes in CPU time.

Anne


More information about the Numpy-discussion mailing list