When parsing a YYYYMMdd date, e.g. 20120405 for 5th April 2012, what is the fastest method?
int year = Integer.parseInt(dateString.substring(0, 4));
int month = Integer.parseInt(dateString.substring(4, 6));
int day = Integer.parseInt(dateString.substring(6));
vs.
int date = Integer.parseInt(dateString)
year = date / 10000;
month = (date % 10000) / 100;
day = date % 100;
mod 10000 for month would be because mod 10000 results in MMdd and the result / 100 is MM
In the first example we do 3 String operations and 3 "parse to int", in the second example we do many things via modulo.
What is faster? Is there an even faster method?
How about (but it would parse an invalid date without saying anything...):
Doing a quick and dirty benchmark with 100,000 iterations warm up and 10,000,000 timed iterations, I get:
I believe the mod method will be faster. By calling the function your creating variable and location instances on the stack and create a heavier solution.
Mod is standard math operator and is likely very optomized.
But as Hunter McMillen said "You should look at the Calendar class API"
As you see below, the performance of the date processing only is relevant when you look at millions of iterations. Instead, you should choose a solution that is easy to read and maintain.
Although you could use
SimpleDateFormat
, it is not reentrant so should be avoided. The best solution is to use the great Joda time classes:If we are talking about your math functions, the first thing to point out is that there were bugs in your math code that I've fixed. That's the problem with doing by hand. That said, the ones that process the string once will be the fastest. A quick test run shows that:
Takes ~800ms while:
Takes ~400ms.
However ... again... you need to take into account that this is after 10 million iterations. This is a perfect example of premature optimization. I'd choose the one that is the most readable and the easiest to maintain. That's why the Joda time answer is the best.
I did a quick benchmark test where both methods were executed 1 million times each. The results clearly show that the modulo method is much faster, as Dilum Ranatunga predicted.
The results don't lie (in ms).
The second will certainly be faster, once you change
mod
to%
and add missing semicolons and fix the divisor in theyear
calculation. That said, I'm finding it hard to picture the application where this is a bottleneck. Just how many times are you parsingYYYYMMdd
dates into their components, without any need to validate them?