It's not a secret anymore!
The moose likes Performance and the fly likes speed of Integer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "speed of Integer" Watch "speed of Integer" New topic

speed of Integer

Paul Wetzel
Ranch Hand

Joined: Nov 02, 1999
Posts: 107
The company I am working for is going to develope a web app that does some stuff. We are evaluating java servlets & c++ cgi. the java & c++ teams are to write some code that checks some conditions of an integer(x) a specific number(i) of times. We are to check to make sure that the integer is :1) not empty, valid number,x > 10, 2 >= x <= 100000,1st digit 3. Here is a function (returns boolean)i wrote that would be called in a for loop. <br />
Since I must check for a valid number I think I need to create an integer object each time and catch a NumberFormatException if not a valid number. No matter what i must execute this function "i" times.
I am sure there is something i could be doing to make this faster, any comments a re much appreciated. The faster this is the less likely the i will have to program in c++. please help save me from that fate. Also please look for a simalar port on dates in the future.
thanks again paul

[This message has been edited by Paul Wetzel (edited November 29, 1999).]
[This message has been edited by Frank Carver (edited November 30, 1999).]
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Before we go any further, are you sure you have the requirements down right?
If you study the tests they can actually be factored down to quite a simple set.
The (x > 10) and (2 <= x <= 100000) checks resolve to a more simple (10 < x <= 100000).
But, the number must start with "3", so the limits are actually (30 <= x <= 39999). A little more study and we can see that any number which starts with "3" and has a length of two, three, four or five characters is valid.
So these rules are "true" for any valid integer which starts with a three and has between two and five digits.
Anyway, here are a few speed-up suggestions.
The first thing you can do is to perform the string tests (length test and first digit '3') before the (relatively expensive) string-to-integer conversion.
Second, you can make sure that you return as quick as possible when you get an answer.
Here's my stab at a quicker solution. Note that I am a very strong believer in automatic testing (your code failed, by the way, you were checking the second character when you used "charAt(1)"), so I have included a simple test harness:

I hope this helps.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Paul Wetzel
Ranch Hand

Joined: Nov 02, 1999
Posts: 107
The test must perform each of the tests/operations so factoring them down to the simplest form is not an option. Now that I think about this more, I need to have all of these tests done on an integer.
[This message has been edited by Paul Wetzel (edited November 30, 1999).]
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
OK. But there's still a question. Do all the tests actually have to be executed, or merely to be present in the code but optimized out by the compiler? For example in:

The n < 2 is completely redundant and will probably be optimized out. I guess I still think that more thought should have been put into the tests to perform. Designing performance benchmarks is not an easy task.
Anyway, the same rules still apply; do the simplest/quickest tests first (in this case the empty-string and starts-with-three tests)
only then convert on an int (and make it a real int, not an Integer), and make sure you order your comparisons so it will return as soon as it knows an answer.
One other thing you might want to try:- throwing an exception is relatively slow (it has to build a full stack trace), so compare the performance of 'try { n = parseInt(s); } catch ...' with a self-written
parser/validator along the lines of:

I don't really know if it'll be any faster, but it might...
Jack Shirazi
Ranch Hand

Joined: Oct 26, 2000
Posts: 96
I'll look at some of the older threads like this one when I have the time.
This kind of test is well known as a biased test. It deliberately aims to test very simple functionality in the two languages. Testing such simple functionality always favours the lower-level language. Do the same test with Assembler vs C (which to my mind is about the difference in level as C to Java), and Assembler will win easily.
Having said that, you can still get really close to C performance, and maybe even beat C depending on the detailed test specification - I've found that simplistic speed test's like this are often specified so that the winner is actually the coder who has watched the most courtroom drama on TV (the spirit of the test is to show C++ is faster, so the wording is critical and allows you to manipulate the test).
To get Java speed up to max, you want to:
1) Avoid Strings completely. Use a char array, or even a byte array if the original data is in that format.
2) Read the number directly character by character, num = num*10 + (nextchar - 48).
3) Don't use Integer objects, use ints directly.
In other words, the optimized Java code will look pretty much identical to the optimized C code. Then run the test with the fastest VM you can find. In addition, let the full test run a couple of times before you start a test run with timing, so that you can get the full effect of things like HotSpot optimizations.
Some of this may seem a bit like cheating, but if the C++ guys are at all competent, you should see what they're doing with their compiler options to wring out every last nanosecond from their test.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
In the meantime, Jack has written an article about this very problem.
You can find it here:
Micro-Tuning Step-by-Step

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: speed of Integer
jQuery in Action, 3rd edition