473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Fast memory copy of 2 areas...

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-

Nov 15 '05 #1
4 1950
Mat wrote:
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[i]);
return 0;
}

Hope this helps.

cheers,
forayer

--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes
Nov 15 '05 #2

"Mat" <he***********@gmail.com> wrote

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.
Nov 15 '05 #3
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.

Nov 15 '05 #4
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.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Alex Vinokur | last post by:
Consider the following statement: n+i, where i = 1 or 0. Is there more fast method for computing n+i than direct computing that sum? -- Alex Vinokur email: alex DOT vinokur AT gmail DOT...
4
by: Evangelista Sami | last post by:
hello all i am implementing an application in which a large number of (char *) is stored into a hash table structure. As there may be collisions, each (char *) inserted into my hash table is...
7
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For...
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
10
by: Markus.Elfring | last post by:
Some APIs/SDKs are available to modify settings like "readable", "writeable" or "executeable". Examples: - http://msdn.microsoft.com/library/en-us/memory/base/memory_protection_constants.asp -...
14
by: Greg Copeland | last post by:
I am running python on VxWorks. In the course of operation, a vxworks tasks writes to a reserved area of memory. I need access to this chunk of memory from within python. Initially I thought I...
4
by: Alexis Gallagher | last post by:
(I tried to post this yesterday but I think my ISP ate it. Apologies if this is a double-post.) Is it possible to do very fast string processing in python? My bioinformatics application needs to...
9
by: Michael Sparks | last post by:
Hi, I'm interested in writing a simple, minimalistic, non persistent (at this stage) software transactional memory (STM) module. The idea being it should be possible to write such a beast in a...
4
by: setesting001 | last post by:
How many of you are running Fast CGI instead of the popular mod_php? I planning to change to fast cgi since my applications need around 15MB memory to handle a single request, so consider if...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.