package com.mindprod.example;
import static java.lang.System.*;
/**
* Test speed of arraycopy.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2013-04-19 initial version
* @since 2013-04-19
*/
public final class TestArraycopySpeed
{
/**
* how many chars to copy in each test
*/
private static int LIMIT = 10000;
/**
* Sample code compare String copy two different ways.
*
* @param args not used.
*
* @noinspection ManualArrayCopy
*/
public static void main( String[] args )
{
char[] source = new char[ LIMIT ];
char[] target = new char[ LIMIT ];
for ( int i = 0; i < LIMIT; i++ )
{
source[ i ] = ( char ) i;
target[ i ] = ( char ) i;
}
int offset = 0;
for ( int chunk = 1; chunk < LIMIT; chunk++ )
{
long p1 = System.nanoTime();
offset = 0;
for ( int j = 0; j < LIMIT / chunk; j++ )
{
System.arraycopy( source, 0, target, offset, chunk );
offset += chunk;
}
long p2 = System.nanoTime();
offset = 0;
for ( int j = 0; j < LIMIT / chunk; j++ )
{
for ( int i = 0; i < chunk; i++ )
{
target[ offset++ ] = source[ i ];
}
}
long p3 = System.nanoTime();
out.println( "chunksize: " + chunk + " arraycopy: " + ( p2 - p1 ) + " char by char: " + ( p3 - p2 ) );
}
}
}