// Illustrates inefficient strings
// badLongString is quadratic
// goodLongString is linear

public class TestString
{
    static String badLongString( int n )
    {
        String result = "";

        for( int i = 0; i < n; i++ )
            result += 'A';

        return result;
    }

    static String goodLongString( int n )
    {
        StringBuffer result = new StringBuffer( );

        for( int i = 0; i < n; i++ )
            result.append( 'A' );

        return new String( result );
    }
        
 
    public static void main( String [ ] args )
    {
        long time = System.currentTimeMillis( );
        String theString;

            // Test them first
        System.out.println( "Should twice print 20 As next" );
        System.out.println( goodLongString( 20 ) );
        System.out.println( badLongString( 20 ) );

            // Time the efficient implementation
        for( int i = 1000; i < 1000000; i *= 2 )
        {
            theString = goodLongString( i );
            long newTime = System.currentTimeMillis( );
            System.out.println( "Time for goodLongString( " + i + " ) is " +
                                 ( newTime - time ) );
            time = newTime;
        }

            // Time the inefficient implementation
        for( int i = 1000; i < 100000; i *= 2 )
        {
            theString = badLongString( i );
            long newTime = System.currentTimeMillis( );
            System.out.println( "Time for goodLongString( " + i + " ) is " +
                                 ( newTime - time ) );
            time = newTime;
        }

    }
}
