-7

In java data types(byte,short,int,float,double,boolean) which is the fastest data type for compilation and computation and why?please list the reason clearly to understand

  • Fastest in what way? To compile? To allocate huge amounts of it? To make computations with? What are you trying to solve? – Petr Janeček Oct 03 '13 at 09:04
  • So that you use the same `data type` for all type's in this world ? because it is the fastest. – Suresh Atta Oct 03 '13 at 09:05
  • What the... *fastest* data type for compilation? What do you mean? I guess you need to ask a proper question. – Eel Lee Oct 03 '13 at 09:05
  • I don't see which one of them is fastest, but in terms of memory, they have difference. – Glenn Oct 03 '13 at 09:05
  • 1
    To be able to answer this question, please get A4 papers, write the data type names onto separate pieces, make paper airplanes out of them, then throw each, measuring the distance and the flight time. Then you'll have the result... Either that, or your question makes as much sense, as this comment did. – ppeterka Oct 03 '13 at 09:06

2 Answers2

7

Why does it matter? Your speed issues are not going to be down to the primitive type you're using, and Premature Optimization Is The Root Of All Evil. I can almost guarantee that the primitive type you're using is not going to affect the speed of your program. Choose your type based on what you need to use it for!

thecoop
  • 45,220
  • 19
  • 132
  • 189
2

The only performance difference between the types is computing in floating point arithemtics vs. integer arithmetics, where computing in integer is clearly and measurably faster.

The rule of thumb - if your data could be naturally represented as integers (e.g. the number of bananas in the store), go for some of the integer types (byte, short, int, long). If your data are represented as real numbers (the amount of kilograms of bananas you bought), go either for float or double.

Every single type has its specifics, you need to get to know their ranges and if you're allocating millions of them, their memory footprint.


EDIT addressing OP's comment:

Could u please tell me why computing in integer is faster?

First off, I have to admit that what I said is not theoretically true. It all depends on the architecture of the computer you'll be running your code on.

Generally though, the statement holds for current modern PC compatible processors. It all boils down to the inner representation of the numbers (search for Two's complement for integer values and Mantissa and exponent for floats) and the inner mechanics of the processors, their computation units and shortcuts for some of the operations. It's a topic for a few classes at least.

The over-simplification tells that counting with integers is easier as they are stored and processed in a much easier way than floats.

Also don't forget that this will differ on different processors and different architectures.

I wrote a simple test showing my results:

private final static int MAX = 1_000_000_000; 

public static void main(String[] args) {
    long time;  // no see

    time = System.currentTimeMillis();
    int intnum = 0;
    for (int i = 0; i < MAX; i++) {
        intnum += i;
    }
    System.out.println("int:\t" + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    long longnum = 0;
    for (int i = 0; i < MAX; i++) {
        longnum += i;
    }
    System.out.println("long:\t" + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    float floatnum = 0;
    for (int i = 0; i < MAX; i++) {
        floatnum += i;
    }
    System.out.println("float:\t" + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    double doublenum = 0;
    for (int i = 0; i < MAX; i++) {
        doublenum += i;
    }
    System.out.println("double:\t" + (System.currentTimeMillis() - time));

    // just to use the values, don't pay attention to the results
    System.out.println();
    System.out.println(intnum);
    System.out.println(longnum);
    System.out.println(floatnum);
    System.out.println(doublenum);
}

The results on my 32-bit processor:

int:    578 ms
long:   1578 ms
float:  3156 ms
double: 3172 ms

While benchmarks like this are hard to get right (and I didn't even try too much), this one fairly precisely describes my real world experience.

Integer is the fastest, then long, then floating point types.

If I had a 64-bit processor, OS and JVM, int and long would probably be on the same level (just like float and double are on my PC).

Community
  • 1
  • 1
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145