commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Anton Tagunov <atagu...@mail.cnt.ru>
Subject Re[2]: [SURVEY] Commons-URI or not?
Date Mon, 30 Jun 2003 09:22:53 GMT
Hi, All!


AT> A recent test that I have carried out showed that .charAt() is
AT> 1.8 - 1.9 times slower then [] on a char array, just in case I attach
AT> the test case.

Oopps, have fogotten the test, here it is in this mail.
And here are the results I got

Results for 'abcdefghijk'
    string version: 426.6
    array version: 242.4
    coeff = 1.75990099009901
Results for 'ab?defghijk'
    string version: 441.6
    array version: 233.3
    coeff = 1.8928418345477926
Results for '??????aaaaaaa'
    string version: 510.8
    array version: 272.4
    coeff = 1.8751835535976504


public class B
{
    public final static String STR1 = "abcdefghijk";
    public final static String STR2 = "ab\u1234defghijk";
    public final static String STR3 = 
            "\u0401\u0402\u0403\u1234\u1234\u1234aaaaaaa";
    public final static int AVG = 10;
    public final static long ITER = 1000000l;
    private static int sum;

    public static final void test( final String str, final int avg,
        final long iter )
    {
        final int len = str.length();
        final char[] arr = str.toCharArray();

        final long t1 = System.currentTimeMillis();

        for ( int a = 0; a < avg; ++a )
        {

            sum = 0;
        
            for ( long i = 0; i < iter; ++i )
            {
                for ( int j = 0; j < len; ++j )
                {
                    sum += str.charAt( j );
                }
            }
        }

        final long t2 = System.currentTimeMillis();

        for ( int a = 0; a < avg; ++a )
        {

            sum = 0;
        
            for ( long i = 0; i < iter; ++i )
            {
                for ( int j = 0; j < len; ++j )
                {
                    sum += arr[ j ];
                }
            }
        }

        final long t3 = System.currentTimeMillis();

        System.out.println( "Results for '" + str + "'\n" +
                "    string version: " + ((double)( t2 - t1 ) / avg ) + "\n" +
                "    array version: "  + ((double)( t3 - t2 ) / avg ) + "\n" +
                "    coeff = " + ((double)( t2 - t1 )/(double)( t3 - t2 )) );
    }

    public static void main( String args[] )
    {
        test( STR1, AVG, ITER );
        test( STR2, AVG, ITER );
        test( STR3, AVG, ITER );
    }
}


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message