Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance paranoia: how much expensive are Float.parseFloat(String), Integer.parseInt(String)?

I am recently using JSON to store configuration parameters for a certain number of sub-classes of the same class. To keep a uniform interface, I have provided the parent class with public void setParameter(String, String) and String getParameter(String) methods. Each sub-class, then, casts the provided parameters to their native type and do some kind of computation using them.

Now, I am wondering: since I already store each parameter inside a HashMap, does it really makes sense to keep a separate field with the right type for each parameter? What is the computational overhead of converting String parameters to their native type each time I need them, given that I need to use them very often?

Thank you
Tunnuz

like image 765
tunnuz Avatar asked Dec 22 '22 15:12

tunnuz


2 Answers

I suggest you test it. It is a fairly expensive operation if you need to do this many, many times but can be less expensive than Double.toString() or Integer.toString() if you used those to create the data in the first place.

I also suggest you only use double unless you know using a float could never ever cause a rounding issue. ;)

It is about as expensive as creating objects, like String or adding an entry to a HashMap. Unless you plan to avoid do this as well, I wouldn't worry about it.

EDIT: Similar to @Stackers' benchmark I would run the test longer and use nanoTime()

int runs = 10000000;
String val = "" + Math.PI;
long start = System.nanoTime();
for (int i = 0; i < runs; i++)
    Float.parseFloat(val);
long time = (System.nanoTime() - start) / runs;
System.out.println("Average Float.parseFloat() time was " + time + " ns.");

long start2 = System.nanoTime();
for (int i = 0; i < runs; i++)
    Double.parseDouble(val);
long time2 = (System.nanoTime() - start2) / runs;
System.out.println("Average Double.parseDouble() time was " + time2 + " ns.");

prints

Average Float.parseFloat() time was 474 ns.
Average Double.parseDouble() time was 431 ns.

BTW: I have function which reads doubles from a direct ByteBuffer which takes 80 ns. It is faster because it doesn't need a String and it doesn't create any objects. However, it is by no means trivial to do this and you have to design your core system to avoid any object creation. ;)

like image 180
Peter Lawrey Avatar answered Dec 29 '22 01:12

Peter Lawrey


Measuring is as easy as:

public class PerfTest {
    public static void main(String[] args) {
        String val = "" + (float) Math.PI;
        long start = System.currentTimeMillis();
        for ( int i = 0 ; i < 100000 ; i++ ) {
            Float.parseFloat( val );
        }
        System.out.println( System.currentTimeMillis() - start + " ms." );
    }
}

62ms for 100.000 iterations.

like image 25
stacker Avatar answered Dec 29 '22 00:12

stacker