Hello, All!
AT> A recent test that I have carried out
Thanks to Dick Zetterberg <[EMAIL PROTECTED]>
DZ> I do not doubt that charAt is slower but I think your test
DZ> should be modified slightly.
DZ> First a question though:
DZ> What JDK version and what platform are you using?
java version "1.4.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-b21)
Java HotSpot(TM) Client VM (build 1.4.1-b21, mixed mode)
Win2K SP3, Celeron around 700-800, 512Mb RAM
DZ> Then for the tests it would be better if you ran each test
DZ> one round first (say 1000 iterations or something) in order
DZ> to warm up the hotstop compiler. And then you run it again
DZ> and this time you time the result.
No problem, I was interested myself :)
Essentially the results did not change 1.83 - 1.95
I also added mixed test, when I do not do charAt(0), charAt(1),
charAt(2) sequentially, but
charAt(0), charAt(5), charAt(1), charAt(4), ...
For this the slow down was 2.02 - 2.22.
The reason I added this test was because there is an obvious
optimization possible in String for the sequential string scan,
that is remembering one or several last chatAt() requests and the
positions in UTF8 that they corresponded to. I wanted to see
what happens if we're a bit less linear and the quotient really
increased a bit.
So, here are the new test results
Results for 'abcdefghijk'
string version: 421
array version: 230
string mixed version: 731
array mixed version: 361
coeff = 1.8304347826086957
mixed coeff = 2.0249307479224377
Results for 'abcdefghijklmnopqrstuvwxyz'
string version: 941
array version: 481
string mixed version: 1652
array mixed version: 741
coeff = 1.9563409563409564
mixed coeff = 2.229419703103914
Results for 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
string version: 1843
array version: 941
string mixed version: 3265
array mixed version: 1462
coeff = 1.9585547290116896
mixed coeff = 2.2332421340629276
Results for
'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz'
string version: 3635
array version: 1853
string mixed version: 6470
array mixed version: 2884
coeff = 1.961683756071236
mixed coeff = 2.2434119278779474
Results for 'ab?defghijk'
string version: 410
array version: 251
string mixed version: 711
array mixed version: 360
coeff = 1.6334661354581674
mixed coeff = 1.975
Results for 'ab?defghijkab?defghijk'
string version: 801
array version: 411
string mixed version: 1392
array mixed version: 651
coeff = 1.948905109489051
mixed coeff = 2.1382488479262673
Results for 'ab?defghijkab?defghijkab?defghijkab?defghijk'
string version: 1572
array version: 791
string mixed version: 2764
array mixed version: 1242
coeff = 1.9873577749683944
mixed coeff = 2.2254428341384864
Results for 'иАБ???aaaaaaa'
string version: 511
array version: 260
string mixed version: 852
array mixed version: 410
coeff = 1.9653846153846153
mixed coeff = 2.078048780487805
Results for 'иАБ???aaaaaaaиАБ???aaaaaaaиАБ???aaaaaaaиАБ???aaaaaaaиАБ???aaaaaaa'
string version: 2273
array version: 1162
string mixed version: 4046
array mixed version: 1823
coeff = 1.9561101549053357
mixed coeff = 2.219418540866703
And here is the new test
public class B
{
public final static String STR[] =
{
"abcdefghijk",
"abcdefghijklmnopqrstuvwxyz",
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz" +
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
"ab\u1234defghijk",
"ab\u1234defghijk" +
"ab\u1234defghijk",
"ab\u1234defghijk" +
"ab\u1234defghijk" +
"ab\u1234defghijk" +
"ab\u1234defghijk",
"\u0401\u0402\u0403\u1234\u1234\u1234aaaaaaa",
"\u0401\u0402\u0403\u1234\u1234\u1234aaaaaaa" +
"\u0401\u0402\u0403\u1234\u1234\u1234aaaaaaa" +
"\u0401\u0402\u0403\u1234\u1234\u1234aaaaaaa" +
"\u0401\u0402\u0403\u1234\u1234\u1234aaaaaaa" +
"\u0401\u0402\u0403\u1234\u1234\u1234aaaaaaa",
};
public final static int WARMUP1 = 1000;
public final static int WARMUP2 = 10;
public final static int ITER = 1000000;
private static int sum;
private static long testCharAt( final String str, final int iter )
{
final int len = str.length();
sum = 0;
final long t1 = System.currentTimeMillis();
for ( int i = 0; i < iter; ++i )
{
for ( int j = 0; j < len; ++j )
{
sum += str.charAt( j );
}
}
final long t2 = System.currentTimeMillis();
return t2 - t1;
}
private static long testMixedCharAt( final String str, final int iter )
{
final int len = str.length();
final int len1 = len - 1;
sum = 0;
final long t1 = System.currentTimeMillis();
for ( int i = 0; i < iter; ++i )
{
for ( int j = 0; j < len; ++j )
{
sum += str.charAt( j );
sum += str.charAt( len1 - j );
}
}
final long t2 = System.currentTimeMillis();
return t2 - t1;
}
private static long testArr( final char[] arr, final int iter )
{
final int len = arr.length;
sum = 0;
final long t1 = System.currentTimeMillis();
for ( int i = 0; i < iter; ++i )
{
for ( int j = 0; j < len; ++j )
{
sum += arr[ j ];
}
}
final long t2 = System.currentTimeMillis();
return t2 - t1;
}
private static long testMixedArr( final char[] arr, final int iter )
{
final int len = arr.length;
final int len1 = len - 1;
sum = 0;
final long t1 = System.currentTimeMillis();
for ( int i = 0; i < iter; ++i )
{
for ( int j = 0; j < len; ++j )
{
sum += arr[ j ];
sum += arr[ len1 - j ];
}
}
final long t2 = System.currentTimeMillis();
return t2 - t1;
}
public static final void test( final String str, final int iter )
{
final int len = str.length();
final char[] arr = str.toCharArray();
/*** round 1 ***/
// warmup the hot spot compiler
for( int a = 0; a < WARMUP2; ++a )
{
testCharAt( str, WARMUP1 );
}
final long time1 = testCharAt( str, iter );
for( int a = 0; a < WARMUP2; ++a )
{
testArr( arr, WARMUP1 );
}
final long time2 = testArr( arr, iter );
/*** round 2 ***/
// warmup the hot spot compiler
for( int a = 0; a < WARMUP2; ++a )
{
testMixedCharAt( str, WARMUP1 );
}
final long time3 = testMixedCharAt( str, iter );
for( int a = 0; a < WARMUP2; ++a )
{
testMixedArr( arr, WARMUP1 );
}
final long time4 = testMixedArr( arr, iter );
System.out.println( "Results for '" + str + "'\n" +
" string version: " + time1 + "\n" +
" array version: " + time2 + "\n" +
" string mixed version: " + time3 + "\n" +
" array mixed version: " + time4 + "\n" +
" coeff = " + ((double)time1/time2) + "\n" +
" mixed coeff = " + ((double)time3/time4) + "\n" +
"" );
}
public static void main( String args[] )
{
for( int k = 0; k < STR.length; ++k )
{
test( STR[k], ITER );
}
}
}
Thanks, Dick Zetterberg!
- Anton
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]