473,419 Members | 1,925 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,419 software developers and data experts.

Fastest way to copy from a one dimension byte array to two dimension int array

I need to copy all the data from a one dimensional byte array to a
specific column in a two dimensional int array. For example:

byte[] byteArray = {4, 5, 6};
int[,] intArray = new int[2, 3];

After copying to the first column in intArray, I want the intArray to
have the following data:

intArray[0, 0] has the value 4
intArray[0, 1] has the value 5
intArray[0, 2] has the value 6

I actually need to copy thousands of bytes, so what is the fastest
solution without using a loop?

Thanks,
Johann

Nov 17 '05 #1
5 2641
Hi Johann,

How many bytes do you really need to copy? If we're talking about
thousands (maybe hundreds of thousands), then performance shouldn't be
an issue. Even if it is, you certainly won't get out of it without using
loops, because the source array element size is different than the
destination. There is no function I'm aware of that would stretch a byte
array to an int array. You might get a little performance increase
through using unsafe code, though. But I would resort to this only in
case the simplest approach would be a terrible performance killer.

HTH,
Stefan

jo*********@yahoo.com wrote:
I need to copy all the data from a one dimensional byte array to a
specific column in a two dimensional int array. For example:

byte[] byteArray = {4, 5, 6};
int[,] intArray = new int[2, 3];

After copying to the first column in intArray, I want the intArray to
have the following data:

intArray[0, 0] has the value 4
intArray[0, 1] has the value 5
intArray[0, 2] has the value 6

I actually need to copy thousands of bytes, so what is the fastest
solution without using a loop?

Thanks,
Johann


Nov 17 '05 #2
I need to copy about 30000 bytes within a single thread but there are
about 9 threads that need to run simultaneously, so the net effect is
270000 bytes. This must all happen well under a second. You're probably
right about having to use unsafe code but that isn't an issue. Anything
that accomplishes the job. Iterating 270000 times sounds kind
horrifying for performance issues.

Nov 17 '05 #3
jo*********@yahoo.com wrote:
I need to copy about 30000 bytes within a single thread but there are
about 9 threads that need to run simultaneously, so the net effect is
270000 bytes. This must all happen well under a second. You're probably
right about having to use unsafe code but that isn't an issue. Anything
that accomplishes the job. Iterating 270000 times sounds kind
horrifying for performance issues.


You have to measure.

I've just done a few benchmark, copying bytes from a byte[270000] to an
int[270, 1000] array, using three methods. In all cases, the source and
destination arrays were preallocated in order to avoid garbage
collections during the benchmark:

[Benchmark]
public static void TestSimple()
{
for (int i = 0; i < src.Length; i++)
dest[i / 1000, i % 1000] = src[i];
}

[Benchmark]
public static void TestDouble()
{
int psrc = 0;

for (int y = 0; y < 270; y++)
{
for (int x = 0; x < 1000; x++)
{
dest[y, x] = src[psrc++];
}
}
}

[Benchmark]
public unsafe static void TestUnsafe()
{
fixed (int* fd = dest)
{
int* pd = fd;

fixed (byte* fs = src)
{
byte* ps = fs;

for (int i = 0; i < 270000; i++)
*pd++ = *ps++;
}
}
}

And the results were as follows:

-------------------------------------------------
Benchmarking 'BenchmarkFX.ArrayTest - Void TestSimple()'
Generating code...
Running benchmark...
Result: Performed 1000 iterations in 8,33078142615884 sec
Single call took: 8,331 ms
-------------------------------------------------
Benchmarking 'BenchmarkFX.ArrayTest - Void TestDouble()'
Generating code...
Running benchmark...
Result: Performed 1000 iterations in 3,05974032866275 sec
Single call took: 3,060 ms
-------------------------------------------------
Benchmarking 'BenchmarkFX.ArrayTest - Void TestUnsafe()'
Generating code...
Running benchmark...
Result: Performed 3000 iterations in 2,56928532893266 sec
Single call took: 856,428 us
Done
As you can see, the biggest performance killer appears to be the div /
mod in the simple case. I think the performance gained through
optimizing the code is negligible (even though it's an order of
magnitude from the simplest case to unsafe code), as long as it's
required to run "well under a second".

All the tests were made using a P4C, running 2.6GHz

HTH,
Stefan
Nov 17 '05 #4
On 14 Jun 2005 04:30:51 -0700, "jo*********@yahoo.com"
<jo*********@yahoo.com> wrote:
I need to copy about 30000 bytes within a single thread but there are
about 9 threads that need to run simultaneously, so the net effect is
270000 bytes. This must all happen well under a second. You're probably
right about having to use unsafe code but that isn't an issue. Anything
that accomplishes the job. Iterating 270000 times sounds kind
horrifying for performance issues.


You are seriously overestimating the time the task takes. The
following code is with managed code. (And I doubt unmanaged code will
make it much faster). If you run the following code you will notice
that it runs quite fast, probably 10 ms at most.

public static void CopyByteToInt(byte[] source, int[,] target, int
column)
{
System.Diagnostics.Trace.Assert(source.Length ==
target.GetLength(1));

for (int i = 0; i < source.Length; i++)
target[column, i] = source[i];
}

public static void Main()
{
byte[] byteArray = new byte[30000];
int[,] intArray = new int[2, 30000];

DateTime start = DateTime.Now;
for (int i = 0; i < 10; i++)
CopyByteToInt(byteArray, intArray, 0);
Console.WriteLine(DateTime.Now - start);
}

--
Marcus Andrén
Nov 17 '05 #5

<jo*********@yahoo.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
I need to copy about 30000 bytes within a single thread but there are
about 9 threads that need to run simultaneously, so the net effect is
270000 bytes. This must all happen well under a second. You're probably
right about having to use unsafe code but that isn't an issue. Anything
that accomplishes the job. Iterating 270000 times sounds kind
horrifying for performance issues.


What do you mean exactly with ... 9 threads that need to run
simultaneously...?
Unless you have a 9 way SMP box, these threads won't run simultaneously,
such number of threads that are CPU bound are useless on a mono CPU box, and
make the whole process to take more time to execute than a single thread.
So make sure that when you realy need them that they are not CPU bound.

Willy.

Nov 17 '05 #6

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

Similar topics

17
by: DraguVaso | last post by:
Hi, I need to find the FASTEST way to get a string in a Loop, that goes from "a" to "ZZZZZZZZZZZZZZZZZ". So it has to go like this: a b .... z
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
4
by: rmorvay | last post by:
I have a requirement to search a multi-dimensional array for an item, then delete the item and "reset" the array so that their are no gaps in the resulting array. I have been trying to figure out...
4
by: Dennis | last post by:
I have several Data Structures, say "mystruct" which contain arrays of bytes, other structures, etc. I then dimension a variable (var1) as "mystruct" and set the various elements var1 to data. I...
5
by: Tales Normando | last post by:
The title says it all. Anyone?
7
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a...
24
by: ThunderMusic | last post by:
Hi, The subject says it all... I want to use a byte and use it as byte* so I can increment the pointer to iterate through it. What is the fastest way of doing so in C#? Thanks ThunderMusic
7
by: lovecreatesbea... | last post by:
Is it always legal to cast expressions of type of multi-dimension array to type of pointer? Including: T to T* , T to T* , T to T* , and so on... For example: int *mtxrot1d(int *p,...
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?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.