Tuesday, April 1, 2014

Converting String to Integer

I ran some tests to see the fastest way to convert a String to Integer (or int)



public class HelloWorld {

  public static int limit = 1000000;
  public static String sint = "9999";

  public static void main(String[] args) {

      long start = System.currentTimeMillis();
      for (int i = 0; i < limit; i++) {
         Integer integer = Integer.valueOf(sint);
      }
      long end = System.currentTimeMillis();

      System.out.println("valueOf took: " + (end - start));


      start = System.currentTimeMillis();
      for (int i = 0; i < limit; i++) {
          int integer = Integer.parseInt(sint);
      }
      end = System.currentTimeMillis();

      System.out.println("parseInt took: " + (end - start));


      start = System.currentTimeMillis();
      for (int i = 0; i < limit; i++) {
          int integer = Ints.tryParse(sint);
      }
      end = System.currentTimeMillis();

      System.out.println("Ints.tryParse took: " + (end - start));


      start = System.currentTimeMillis();
      for (int i = 0; i < limit; i++) {
          Integer integer = NumberUtils.createInteger(sint);
      }
      end = System.currentTimeMillis();

      System.out.println("numberUtils.createInteger took: " + (end - start));

      start = System.currentTimeMillis();
      for (int i = 0; i < limit; i++) {
          int integer = NumberUtils.toInt(sint);
      }
      end = System.currentTimeMillis();

      System.out.println("numberUtils.toInt took: " + (end - start));

  }
}

My results were:
valueOf took: 77
parseInt took: 61
Ints.tryParse took: 117
numberUtils.createInteger took: 169
numberUtils.toInt took: 63

So the summary is

If you can get by using an int, use Integer.parseInt.
If you absolutely need an Integer, use Integer.valueOf
If you need the convenience of not handling exceptions when you parse, or if you are unsure of the format of the input (i.e its a string that need not be a number) use Ints.tryParse

Why use Ints.tryparse instead of Integer.parseInt?

Because I ran some other code with bad values:

public class HelloWorld {

    public static int limit = 1000000;
    public static String sint = "abcd";

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        for (int i = 0; i < limit; i++) {
            try {
                Integer.parseInt(sint);
            } catch (NumberFormatException e) {
                // do nothing
            }
        }
        long end = System.currentTimeMillis();

        System.out.println("parseInt took: " + (end - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < limit; i++) {
            Ints.tryParse(sint);
        }
        end = System.currentTimeMillis();

        System.out.println("Ints.tryParse took: " + (end - start));

    }
}

Here the results were:
parseInt took: 2630
Ints.tryParse took: 87

This should be quite obvious as throwing exceptions is a huge performance hit.

No comments:

Post a Comment