Fast memory copy of 2 areas...

M

Mat

Hi all.
This is my situation:

- I have 3 arrays, for example:
unsigned char myArray1[16] =
{
11, 12, 13, 14,
21, 22, 23, 24,
31, 32, 33, 34,
41, 42, 43, 44
};
unsigned char myArray2[16] =
{ /* like myArray1, different elements of course */ }
unsigned char myArray3[32];
( in my real code the array size is bigger... )

- I would like to merge myArray1 and myArray2 in myArray3 in this way:
myArray3[0] = myArray1[0]
myArray3[1] = myArray1[1]
myArray3[2] = myArray1[2]
myArray3[3] = myArray1[3]
myArray3[4] = myArray2[0]
myArray3[5] = myArray2[1]
myArray3[6] = myArray2[2]
myArray3[7] = myArray2[3]
// ...
So I need to copy alterned lines of n elements (n fixed) from the
arrays and I would like to do without a loop... I need to do this
operation very fast and without using many resources (CPU).

Is it possible?
Any good ideas?

[OT]Ok ok, I know it's OT but my OS is Linux so if someone knows a
system-dependent solution is welcome too...[/OT]

Tnx in advance.
-Mat-
 
V

Vimal Aravindashan

Mat said:
Hi all.
This is my situation:

- I have 3 arrays, for example:
unsigned char myArray1[16] =
{
11, 12, 13, 14,
21, 22, 23, 24,
31, 32, 33, 34,
41, 42, 43, 44
};
unsigned char myArray2[16] =
{ /* like myArray1, different elements of course */ }
unsigned char myArray3[32];
( in my real code the array size is bigger... )

- I would like to merge myArray1 and myArray2 in myArray3 in this way:
myArray3[0] = myArray1[0]
myArray3[1] = myArray1[1]
myArray3[2] = myArray1[2]
myArray3[3] = myArray1[3]
myArray3[4] = myArray2[0]
myArray3[5] = myArray2[1]
myArray3[6] = myArray2[2]
myArray3[7] = myArray2[3]
// ...
So I need to copy alterned lines of n elements (n fixed) from the
arrays and I would like to do without a loop... I need to do this
operation very fast and without using many resources (CPU).
[snip]

I think you are looking for something similar to the following:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int a[8];
int h[4]={0,1,2,3}, l[4]={7,6,5,4};
int i;

memcpy(&a[0], l, sizeof(l));
memcpy(&a[4], h, sizeof(h));

for(i=0; i<8; i++)
printf("%d,", a);
return 0;
}

Hope this helps.

cheers,
forayer
 
M

Malcolm

Mat said:
So I need to copy alterned lines of n elements (n fixed) from the
arrays and I would like to do without a loop... I need to do this
operation very fast and without using many resources (CPU).
Why do you wnat to avoid the loop?
On many platforms, loops are extremely well optimised and often faster than
linear code.

The code is trivial, so what you must do is write it in a few ways, profile
(run the code a thousand times in a loop if resolution isn't good enough),
and choose the fastest. That might be linear code like you posted,it might
be a call to memcpy(), or it might be a loop.
 
M

Mat

Vimal Aravindashan:
memcpy(&a[0], l, sizeof(l));
memcpy(&a[4], h, sizeof(h));

Tnx for your reply Vimal but if I use memcpy in this way I need a loop
to fill every alterned lines and this is not what I want... I don't
have only the first line to fill.
 
M

Mat

Malcolm:
Why do you wnat to avoid the loop?
On many platforms, loops are extremely well optimised and often faster than
linear code.

I would like to avoid loops because the buffers I work with are 400kb
each and I have to do this operation 10 - 20 times in a second... it
could be too much for the CPU.
The code is trivial, so what you must do is write it in a few ways, profile
(run the code a thousand times in a loop if resolution isn't good enough),
and choose the fastest. That might be linear code like you posted,it might
be a call to memcpy(), or it might be a loop.

I hoped in a strange way to use memcpy :)
To introduce an offset every n bytes...

But I think I will avoid this operation and I probably will work with 2
buffers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top