I
I
Igor Ivanov2016-02-08 23:24:57
C++ / C#
Igor Ivanov, 2016-02-08 23:24:57

Fastest way to shift array elements by one position?

There is an array of data:
long long *Data=new long long[DATA_COUNT];
DATA_COUNT can be thousands and tens of thousands.
It is necessary at each iteration to shift the array elements by one position to the left (overwriting the very first element).
For example:
Before - 0, 1, 2, 3, 4, 5
Now - 1, 2, 3, 4, 5, X - where X is the location for the next (newer) data.
I do like this:

memmove (&Data[0], &Data[1], (DATA_COUNT-1)*sizeof(long long));

Is this the fastest way? Or eat faster?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-02-08
@ksey3000

In fact, such a structure is called a queue (FIFO) and is implemented on the basis of two pointers - reading (beginning of the queue) and writing (end of the queue).

V
Vladimir Martyanov, 2016-02-08
@vilgeforce

It's much faster to change the pointer to the "beginning" of the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question