Thursday, March 22, 2007

Performance: clone() vs. new GregorianCalendar()

Performance for getting a new Calendar Object

::Environment::
- JDK "1.6.0" (build 1.6.0-b105)
- Linux (CentOS 4.4)

::Result::
loopby Clone
by MilliSec
by Setter
by Constructor
100
11ms
11ms
5ms
2ms
1000
59ms
61ms
9ms
6ms
10000
85ms
71ms
59ms
19ms
30000
178ms
136ms
102ms
28ms
60000
279ms
249ms
189ms
42ms



::Code::
By Clone

public long byClone(final int loop) {
final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = (Calendar) Calendar.getInstance().clone();
}
final long end = System.currentTimeMillis();
return end - start;
}


By Setting MilliSeconds

public long byMilliSeconds(final int loop) {
final Calendar now = Calendar.getInstance();
final TimeZone tz = now.getTimeZone();
final long mill = now.getTimeInMillis();

final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = new GregorianCalendar(tz);
cal.setTimeInMillis(mill);
}
final long end = System.currentTimeMillis();

return end - start;
}


By Setting Each Field

public long bySetter(final int loop) {
final Calendar now = Calendar.getInstance();
final int currentYear = now.get(Calendar.YEAR);
final int currentMonth = now.get(Calendar.MONTH);
final int currentDate = now.get(Calendar.DATE);
final int currentHour = now.get(Calendar.HOUR);
final int currentMinute = now.get(Calendar.MINUTE);
final int currentSecond = now.get(Calendar.SECOND);
final int currentMilliSecond = now.get(Calendar.MILLISECOND);
final TimeZone tz = now.getTimeZone();

final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = new GregorianCalendar(tz);
cal.set(currentYear, currentMonth, currentDate, currentHour, currentMinute, currentSecond);
cal.set(Calendar.MILLISECOND, currentMilliSecond);
}
final long end = System.currentTimeMillis();

return end - start;
}


By Constructor

public long byConstructor(final int loop) {
final Calendar now = Calendar.getInstance();
final int currentYear = now.get(Calendar.YEAR);
final int currentMonth = now.get(Calendar.MONTH);
final int currentDate = now.get(Calendar.DATE);
final int currentHour = now.get(Calendar.HOUR);
final int currentMinute = now.get(Calendar.MINUTE);
final int currentSecond = now.get(Calendar.SECOND);
final int currentMilliSecond = now.get(Calendar.MILLISECOND);
final TimeZone tz = now.getTimeZone();

final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = new GregorianCalendar(currentYear, currentMonth, currentDate, currentHour, currentMinute, currentSecond);
cal.set(Calendar.MILLISECOND, currentMilliSecond);
cal.setTimeZone(tz);
}
final long end = System.currentTimeMillis();

return end - start;
}

4 comments:

Anonymous said...

Thanks. We hit similar problems and googled this post. This gives us an idea on how to resolve a problem. We have a modified version of your "clone" call and this is the fastest - the Calendar.getInstance is placed outside of the loop (like your other test cases) and inside the loop it's simply calling clone. This turns out to be the fastest way to get a dummy calendar instance.

Arigatou Gozaimashita!

Anonymous said...

This blog is great! Thanks for your hard work on it.

Anonymous said...

[u][b]Xrumer[/b][/u]

[b]Xrumer SEO Professionals

As Xrumer experts, we secure been using [url=http://www.xrumer-seo.com]Xrumer[/url] for a long time conditions and grasp how to harness the massive power of Xrumer and build it into a Spondulix machine.

We also provide the cheapest prices on the market. Many competitors desire cost 2x or square 3x and a end of the term 5x what we debt you. But we maintain in providing prominent help at a tearful affordable rate. The unbroken point of purchasing Xrumer blasts is because it is a cheaper variant to buying Xrumer. So we focusing to support that mental activity in recollection and outfit you with the cheapest standing possible.

Not simply do we cause the unexcelled prices but our turnaround time for your Xrumer posting is super fast. We drive pull someone's leg your posting done to come you certain it.

We also cater you with a full log of successful posts on contrary forums. So that you can notice for yourself the power of Xrumer and how we have harnessed it to benefit your site.[/b]


[b]Search Engine Optimization

Using Xrumer you can expect to distinguish thousands upon thousands of backlinks for your site. Many of the forums that your Site you will be posted on get exalted PageRank. Having your association on these sites can really expropriate found up some top quality endorse links and uncommonly riding-boot your Alexa Rating and Google PageRank rating through the roof.

This is making your put more and more popular. And with this inflate in popularity as well as PageRank you can envisage to witness your place definitely superiority expensive in those Search Mechanism Results.
Above

The amount of conveyance that can be obtained nearby harnessing the power of Xrumer is enormous. You are publishing your plat to tens of thousands of forums. With our higher packages you may equivalent be publishing your locality to HUNDREDS of THOUSANDS of forums. Imagine 1 mail on a popular forum disposition by enter 1000 or so views, with signify 100 of those people visiting your site. Now devise tens of thousands of posts on popular forums all getting 1000 views each. Your traffic ordain associate at the end of one's tether with the roof.

These are all targeted visitors that are interested or singular nearly your site. Imagine how divers sales or leads you can achieve with this titanic figure up of targeted visitors. You are in fact stumbling upon a goldmine bright to be picked and profited from.

Remember, Transport is Money.
[/b]

TRAVERSE B RECOVER YOUR TWOPENNY DEFAME TODAY:


http://www.xrumer-seo.com

Anonymous said...

Just what I was looking for! Many thx