views:

109

answers:

2

I heard that using shorts on 32bit system is just more inefficient than using ints. Is this the same for ints on a 64bit system?

Python recently(?) basically merged ints with long and has basically a single datatype long, right? If you are sure that your app. will only run on 64bit then, is it even conceivable (potentially a good idea) to use long for everything in Java?

+2  A: 

A Python long has arbitrary precision, it's not 64-bit. Python 3 changed long to int, so now there is only one integral type of arbitrary precision, this saves a lot of work for the programmer. Java's int is 32-bit, it's long is 64-bit. A 64-bit processor can generally perform better than a 32-bit processor on a 64-bit integer.

Using 32-bit integers on a 64-bit platform isn't expensive, at least not in x64. It makes no sense to choose a 32-bit over 64-bit int or vise-versa in Java just for performance reasons. If you used two 32-bit ints instead of one 64-bit int, it would be slower no matter what. Similarily, if you use 64-bit where you could have used 32-bit, it will be slower on some platforms. In other words, use the right data type for your problem domain.

Longpoke
Exactly, let the problem choose the int size for you, not micro-local issues like platform int size. Don't forget, this is Java. Your class files may run anywhere, and the once there the JIT will optimize as necessary and smart. If, for some bizarre reason, the local platform could more efficiently deal with ints as 64-bit quantities, then the JIT could under the hood arrange for ints to be 64-bit quantities. As far as your program is concerned, int is 32 bits. Let smart compilers do their job.
GregS
Yes, and optimization should be for JVM, you want your code to run faster on JVM (which runs on anything), not ARM or x86. A benevolent side effect is that if there is a hardware JVM, it will run your code at optimal speed.
Longpoke
+1  A: 

If you are sure that your app. will only run on 64bit then, is it even conceivable (potentially a good idea) to use long for everything in Java?

Bad idea, IMO

Even if there is a difference between using int and long on a 64-bit JVM (which I think is highly doubtful), it won't be significant enough over the entire application, including the libraries that you depend on to warrant using long for everything.

And there are some potential problems:

  • You will use more memory, at least for long[] versus int[].
  • You will regret it if your assumption about never needing to run on a 32-bit platform turns out to be wrong.

(The reason I think there won't be a significant performance difference is that the issue, if there is one, will be fetching and storing non-aligned int-32s. But if that is so, the JVM designers are likely to ensure that int-32 fields are always aligned on 64-bit word addresses. JVMs typically do this already with int-8 and int-16 on 32bit architectures ... )

Stephen C