Question on efficient copy from one array to another, code included

B

barry

Hi people, the following code works but my question is is there a
(far) more efficient way of populating the array _byte from the array
bufferB. In the demo application below there I am only copying over 8
bytes, but in the real program I am copying 100's.
Thank you
Barry

package com.somewhere;

public class Class1
{
private static byte[] _byte=null;

public static void popByte(byte[] bufferB,int noOutOfTotalToCopy, int
start)
{
_byte=new byte[noOutOfTotalToCopy];
//is there a more efficient way of copying from bufferB to _byte?
int finish=start+noOutOfTotalToCopy;
for (int i=start;i<finish;i++)
_byte[i-start]=bufferB;
}

public static void main(String[] args)
{
byte[] test={1,3,2,56,23,3,6,23,5,4,2,5,76,6,4,2,17,19,32,32,34};
popByte(test,8,3);
}
}
 
E

Eric Sosman

barry said:
Hi people, the following code works but my question is is there a
(far) more efficient way of populating the array _byte from the array
bufferB. In the demo application below there I am only copying over 8
bytes, but in the real program I am copying 100's.
Thank you
Barry

package com.somewhere;

public class Class1
{
private static byte[] _byte=null;

public static void popByte(byte[] bufferB,int noOutOfTotalToCopy, int
start)
{
_byte=new byte[noOutOfTotalToCopy];
//is there a more efficient way of copying from bufferB to _byte?
int finish=start+noOutOfTotalToCopy;
for (int i=start;i<finish;i++)
_byte[i-start]=bufferB;
}

public static void main(String[] args)
{
byte[] test={1,3,2,56,23,3,6,23,5,4,2,5,76,6,4,2,17,19,32,32,34};
popByte(test,8,3);
}
}


Try System.arraycopy().
 
G

Gordon Beaton

Hi people, the following code works but my question is is there a
(far) more efficient way of populating the array _byte from the
array bufferB. In the demo application below there I am only copying
over 8 bytes, but in the real program I am copying 100's.

I wouldn't worry about it until you start copying hundreds of
thousands of bytes. The only change I'd make in that case would be to
remove the calculation of i-start at every position, and use a second
index instead.

BTW did you not know about System.arraycopy()? It's implemented in
native code and is presumably faster when you copy a large enough
array.

/gordon
 
M

Michael Borgwardt

Eric said:
Try System.arraycopy().

Note that Try System.arraycopy() can in fact be *slower* than a "manual" copy,
because the native implementation which possibly includes a system call and thus
a context switch, incurs a considerable overhead. In most cases it is only faster
for really big arrays upwards of 10,000 elements.
 
B

barry

Gordon & Eric - I didn't know about System.arraycopy and have now used
it. Thanks. Barry.
 
R

Roedy Green

Note that Try System.arraycopy() can in fact be *slower* than a "manual" copy,
because the native implementation which possibly includes a system call and thus
a context switch, incurs a considerable overhead. In most cases it is only faster
for really big arrays upwards of 10,000 elements.

Back a few years there were a number of benchmarks posted showing
doing loop copies by hand were usually faster. This would be even more
so with an optimising compiler.
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top