Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Are ints better?

 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are ints any better to use than other integral types? (i.e. byte, short, long) I know that in C/C++ most programmers use 32-bit numbers (which in Java would be int) since most processors are 32-bit, and thus an operation using a 32-bit number would (could?) be faster/efficient/better than one using a number of a different size... Is there any truth to this in Java? With the VM and all I'm not sure how big an impact this would have on speed of execution...
-Nate
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On my tests, an int, byte, and short are about the same. Long, double and float are slower due to their size and, presumably, precision issues.
Here is some code I had laying around and some of its output on JDK 1.2.1.

Admittedly, this is quick and dirty. Your mileage can vary on other VMs.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Peter
You should correct the message here before
Example:
public static void forint(int val)
{ int j=0;
for (int i=0;i j += i; }
a lot of stuff is missing!
All over the example.
Peter
 
Peter Gragert
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I corrected the source (what was probably meant)
And got this:
With Suns JDK input parameter 100000
Numbers were different
forint 260
forlong 621
fordouble 892
forfloat 590
forshort 281
forbyte 531
With Symantecs Java but input was 10000000
forint 250
forlong 241
fordouble 270
forfloat 240
forshort 251
forbyte 250
So all nearly the same and a 100 times faster.
You see?
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Gragert,
Peter Haggar's program is correct, the UBB software is treating is i < val statement as an HTML tag if you don't put a space between the "<" and the variabl "val". If you click on the edit button to his post, you can do a cut-n-paste from his program and you'll see that it's complete.

-Peter Tran
[This message has been edited by Peter Tran (edited February 26, 2001).]
 
Jack Shirazi
Author
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My tests in a variety of environments show that sometimes they are the same, but more often int is faster. Also, Peter's test was for local variables. Try it also with heap variables (instance variables of objects) and you should see a difference.
There is no specific guarantee of anything in the way of one type is faster than another from Java. But in general the data types shorter than int get widened to int for many arithmetic operations (Peter's test here used operations that were not widened), and double and long assignments and reads are not guaranteed atomic, where ints are.
I would recommend using int rather than other data types for speed, where that doesn't change the logic of the code.
--Jack Shirazi http://www.JavaPerformanceTuning.com/
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies for the mangled code. This is not the first time the brackets got me. I would have thought enclosing it in would alleviate the problem...but I guess not.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic