90Fermer92
Pen^2Le 25/02/2011 à 11:45
Pen^2 (./68) :
iwannabeamaki (./66) :
Tu as un exemple ?
De ? D'exception en attribut ?
Ben, pas spécifiquement là tout de suite, mais ça prendrait trente seconde à coder. Tu veux que je teste les perfs ? Je sais pas si j'aurai le courage ce soir grin





Bon, voici le code de test, puis les résultats :
/**
 *
 */


package exceptions_test ;


public class Main
{
   public int methodReturnInt( String param )
   {
      if ( param == null ) {
         return -1 ;
      }
      // traitement
      return 0 ;
   }





   public void methodThrowsException( String param )
         throws Exception
   {
      if ( param == null ) {
         throw new Exception("La valeur 'null' n'est pas acceptée") ;
      }
      // traitement
   }





   private static final Exception e= new Exception("La valeur 'null' n'est pas acceptée") ;
   public void methodThrowsStaticException( String param )
         throws Exception
   {
      if ( param == null ) {
         throw e ;
      }
      // traitement
   }












   public void testPerf( String param, int iteration )
   {
      long start ;
      long duree ;

      System.out.println(" * " + iteration + " appels de methode avec '"+ param + "': ") ;


      System.out.print("\t methodReturnInt        : ") ;
      start= System.currentTimeMillis() ;
      for ( int i= 0 ; i < iteration ; i++ ) {
         switch ( methodReturnInt(param) ) {
            case -1:
               // Traitement Erreur
               break ;
            case 0:
               // Traitement OK
               break ;
         }
      }
      duree= System.currentTimeMillis() - start ;
      System.out.println(duree + " ms.") ;



      System.out.print("\t methodThrowsException  : ") ;
      start= System.currentTimeMillis() ;
      for ( int i= 0 ; i < iteration ; i++ ) {
         try {
            methodThrowsException(param) ;
            // Traitement OK
         }
         catch ( Exception e ) {
            // Traitement Erreur
         }
      }
      duree= System.currentTimeMillis() - start ;
      System.out.println(duree + " ms.") ;




      System.out.print("\t methodThrowsStaticException  : ") ;
      start= System.currentTimeMillis() ;
      for ( int i= 0 ; i < iteration ; i++ ) {
         try {
            methodThrowsStaticException(param) ;
            // Traitement OK
         }
         catch ( Exception e ) {
            // Traitement Erreur
         }
      }
      duree= System.currentTimeMillis() - start ;
      System.out.println(duree + " ms.") ;
      System.out.println() ;
   }





   /**
    * @param args
    */
   public static void main( String[] args )
   {
      Main obj= new Main() ;

      int iteration= 5000000 ;

      obj.testPerf("string", iteration) ;
      obj.testPerf(null, iteration) ;
   }

}






 * 5000000 appels de methode avec 'string': 
	 methodReturnInt        : 19 ms.
	 methodThrowsException  : 10 ms.
	 methodThrowsStaticException  : 8 ms.

 * 5000000 appels de methode avec 'null': 
	 methodReturnInt        : 9 ms.
	 methodThrowsException  : 4739 ms.
	 methodThrowsStaticException  : 700 ms.


Alors, OK, c'est plus lent, mais déjà que pour le test c'est pas dramatique.... Et quand on voit tous les avantages d'une Exception par rapport à un code d'erreur moisi... cheeky