The Artima Developer Community
Sponsored Link

Java Answers Forum
WHICH ONE IS FASTER

5 replies on 1 page. Most recent reply: Mar 11, 2003 10:05 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

WHICH ONE IS FASTER Posted: Mar 11, 2003 3:22 AM
Reply to this message Reply
Advertisement
class Test
{

long l1 = 0L;

long l2 = (long)0;
}
which one of the two is faster assignment??


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: WHICH ONE IS FASTER Posted: Mar 11, 2003 12:01 PM
Reply to this message Reply
The casting method will be slightly slower, but it is inconsequential compared to any calculation you'll be doing. In other words, it scarcely matters, performance wise.

However, the whole purpose for being able to specify the type of literals with the suffixed letter is so you can do things like "long l1 = 0L;", whereas casting is normally used in cases where it is really needed. So not only is this syntax faster, but it is the correct one to use for initialization.

By the way, in this particular case "long l3 = 1;" works just fine, so the whole point is somewhat moot.

Jay Feng

Posts: 7
Nickname: mcsquared
Registered: Mar, 2003

Re: WHICH ONE IS FASTER Posted: Mar 11, 2003 4:27 PM
Reply to this message Reply
Performance-wise, they're equivalent.
Here's the byte code from your source code:
-------original code --------
class Test
{

long l1 = 0L;

long l2 = (long)0;
}
---- end of original code------

Compiled from Test.java

class Test extends java.lang.Object {
long l1;
long l2;
Test();
}

Method Test()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 aload_0
5 lconst_0
6 putfield #2 <Field long l1>
9 aload_0
10 lconst_0
11 putfield #3 <Field long l2>
14 return

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: WHICH ONE IS FASTER Posted: Mar 11, 2003 6:58 PM
Reply to this message Reply
I think it depends on the compiler. I compiled a little test with Jikes and the casting version was consistently slower. With JavaC they were equivalent (but the compiling was a lot slower!).

Dhrubo

Posts: 32
Nickname: dhrubo
Registered: Mar, 2003

Re: WHICH ONE IS FASTER Posted: Mar 11, 2003 8:52 PM
Reply to this message Reply
Can ya tell me the tool which helped ya guys to see the pnemonics of the bytecodes

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: WHICH ONE IS FASTER Posted: Mar 11, 2003 10:05 PM
Reply to this message Reply
javap -c ClassName will do the trick (it comes with the jdk).

Flat View: This topic has 5 replies on 1 page
Topic: Trouble with Java Game Previous Topic   Next Topic Topic: Interface

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use