Hello,
It's me again, well, I've had a look too to the Java implementation
of the isByte method and I took care of your remarks to reimplement
it (thank you). So here are the results:

old method from Commons Validator :
    public static boolean isByte(String value) {
       return formatByte(value) != null;
    }

    public static Byte formatByte(String value) {
       Byte result = null;

       try {
          result = new Byte(value);
       } catch (Exception e) {
       }

       return result;
    }

And the new one which do exactly what is needed, that's to
say return true or false and doesn't throw any exception:

    public static final boolean isByte( String s )
    {
          // should never happen in JOE
          if( s == null )
              return false;

          int result = 0;
          boolean negative = false;
          int i = 0;
          int max = s.length();
          int limit;
          int multmin;
          int digit;

          if( max > 0 )
          {
              if( s.charAt( 0 ) == '-' )
              {
                negative = true;
                limit = Integer.MIN_VALUE;
                i++;
              }
              else
              {
                limit = -Integer.MAX_VALUE;
              }

              multmin = limit / 10; // radix
              if( i < max )
              {
                digit = Character.digit( s.charAt( i++ ), 10 ); // radix
                if( digit < 0 )
                    return false;
                else
                result = -digit;
              }

              while( i < max )
              {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),10); // radix
                if( digit < 0 )
                    return false;

                if( result < multmin )
                    return false;

                result *= 10; // radix

                if( result < limit + digit )
                    return false;

                result -= digit;
              }
          }
          else
              return false;

          if( negative )
          {
              if( i > 1 )
                return !(result < Byte.MIN_VALUE || result > Byte.MAX_VALUE );
              else
                return false; /* Only got "-" */
          }
          else
              return !(-result < Byte.MIN_VALUE || -result > Byte.MAX_VALUE );
    }

And the results for this test:

        System.out.println((new java.util.Date()).getTime()+"");
        for( int i = 0; i < 100000; i++ )
        {
            isByteApache( "300" );
            isByteApache( "5" );
        }
        System.out.println((new java.util.Date()).getTime()+"");
        for( int i = 0; i < 100000; i++ )
        {
            isByte( "300" );
            isByte( "5" );
        }
        System.out.println((new java.util.Date()).getTime()+"");
        if( isByteApache( "300" ) )
            throw new IllegalStateException("problem");
        if( !isByteApache( "5" ))
            throw new IllegalStateException("problem");
        if( isByte( "300" ) )
            throw new IllegalStateException("problem");
        if( !isByte( "5" ))
            throw new IllegalStateException("problem");
        if( isByte( "-300" ) )
            throw new IllegalStateException("problem");
        if( !isByte( "-5" ))
            throw new IllegalStateException("problem");


2093ms for the first loop and... 90ms for the second one => 25 times
faster!

Thank you again :-)
Loïc

> ps: If you should read something about Knuth, read only his most famous
> quote. really.

Which is...? (the title please)

> With Joe, you're taking the optimization step by the wrong end IMHO

What do you mean by this? Tell me please :-)




Extranet
[EMAIL PROTECTED] - 07/11/2002 17:19


Veuillez répondre à [EMAIL PROTECTED]
Pour : ant-dev

cc :


Objet :     Re: Réf. : RE: Réf. : Re: Commons Validator


----- Original Message -----
From: <[EMAIL PROTECTED]>

>Well it's just about 8% faster:
[...]

ah.

Byte.parseByte(String) is faster, you avoid an allocation.
Byte.parseByte(String, 10) is even faster you avoid a method call

 int i = Integer.parseInt(s, radix);
 return  !(i < Byte.MIN_VALUE || i > MAX_VALUE)
is even more faster. you avoid a method call and and an exception

and yet I suggest to copy/paste the Integer.parseInt method and use your
own
by remove the exceptions to make it even more faster by removing yet
another
method call and exception.

>ps: don't have the time to make a patch (sorry)

I see. Time problem.

ps: If you should read something about Knuth, read only his most famous
quote. really.
With Joe, you're taking the optimization step by the wrong end IMHO


--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@;jakarta.apache.org>










This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
immediately notify the sender. Any use not in accord with 
its purpose, any dissemination or disclosure, either whole 
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message. 
BNP PARIBAS (and its subsidiaries) shall (will) not 
therefore be liable for the message if modified. 

                ---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le 
"message") sont etablis a l'intention exclusive de ses 
destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le detruire et d'en avertir 
immediatement l'expediteur. Toute utilisation de ce 
message non conforme a sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf 
autorisation expresse. L'internet ne permettant pas 
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce 
message, dans l'hypothese ou il aurait ete modifie.


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

Reply via email to