473,796 Members | 2,640 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2662
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*********@yah oo.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*********@yah oo.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.Ar rayTest - Void TestSimple()'
Generating code...
Running benchmark...
Result: Performed 1000 iterations in 8,3307814261588 4 sec
Single call took: 8,331 ms
-------------------------------------------------
Benchmarking 'BenchmarkFX.Ar rayTest - Void TestDouble()'
Generating code...
Running benchmark...
Result: Performed 1000 iterations in 3,0597403286627 5 sec
Single call took: 3,060 ms
-------------------------------------------------
Benchmarking 'BenchmarkFX.Ar rayTest - Void TestUnsafe()'
Generating code...
Running benchmark...
Result: Performed 3000 iterations in 2,5692853289326 6 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*********@ya hoo.com"
<jo*********@ya hoo.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(b yte[] source, int[,] target, int
column)
{
System.Diagnost ics.Trace.Asser t(source.Length ==
target.GetLengt h(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(b yteArray, intArray, 0);
Console.WriteLi ne(DateTime.Now - start);
}

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

<jo*********@ya hoo.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.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
2334
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
8828
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
4
4337
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
5893
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 how to use the Array.Copy method to accomplish this but it seems to only work for single dimension arrays. Here is a code sample: 'dim arrTest(600,3) as string 'Actual array dimension needed dim arrTest(600) as string 'Used only in this test...
4
12463
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 then dimension another variable (var1save) as "mystruct" and set var1save = var1 with the intent of changing var1 data elements and subsequently reasigning var1 back to it's original values of var1save. VB.Net makes only a shallow copy of var1...
5
4503
by: Tales Normando | last post by:
The title says it all. Anyone?
7
6045
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 calculation and update the picture. i currently use the GDI SetPixel method on each pixel of the Pictureboxes image. This is proving far to slow, about 1.5 seconds on average. This app needs to display the update as fast as possible. Has anyone got any...
24
2294
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
3209
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, int n);
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.