Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Which is better... performance- and memory-wise?

Reply
Thread Tools

Which is better... performance- and memory-wise?

 
 
Chris Uppal
Guest
Posts: n/a
 
      11-25-2004
xarax wrote:

> > Not /quite/ identical, there's a good chance that the compiler will
> > generate explicit bytecode for the assignment in:
> >
> > Object o = null;
> >
> > IIRC, the java 1.4.x series compilers did so (I haven't yet looked at
> > the output from the 1.5.0 compiler in cases like this). Of course,
> > there's then the JITer to consider...

>
> Actually, earlier compilers did NOT generate the
> assignment, because the compiler noticed that the
> initial value was the same as the default value.


But by "earlier compilers" you mean 1.3 series or earlier ? I remember
noticing the described behaviour in 1.4 series. Not that it matters -- this is
nitpicking about nitpicking about nitpicking


> Also, JIT'd code is supposedly debuggable now, so
> an explicit assignment of a default value should
> not get optimized away by the JIT.


It's probably worth clarifying this a little -- it could be misinterpreted. As
I understand it, the JITer is still allowed to remove the assignment to null
(and for all I know that's what it does), it's just that it's required to /put
it back/ if you place a breakpoint on it. (Chances are it'll put it back if
you place a breakpoint anywhere in that method -- but that's an implementation
detail and I'm /very/ sketchy on the details).

-- chris



 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Daniel_Sj=F6blom?=
Guest
Posts: n/a
 
      11-25-2004
Chris Uppal wrote:
> xarax wrote:
>
>>Also, JIT'd code is supposedly debuggable now, so
>>an explicit assignment of a default value should
>>not get optimized away by the JIT.

>
>
> It's probably worth clarifying this a little -- it could be misinterpreted. As
> I understand it, the JITer is still allowed to remove the assignment to null
> (and for all I know that's what it does), it's just that it's required to /put
> it back/ if you place a breakpoint on it. (Chances are it'll put it back if
> you place a breakpoint anywhere in that method -- but that's an implementation
> detail and I'm /very/ sketchy on the details).


I created a little test class, and ran it through a little JVMTI agent
that dumps the JIT-generated native code, and I can confirm that at
least on the client 1.5.0 JVM on x86 linux, the explicit assignment was
not removed in the JIT-ed code. That was the only difference between the
compiled code of the two different approaches.

The server VM on the same JVM did generate equivalent code for both
approaches. I suspect the client VM does not do any dead code elimination.

--
Daniel Sj÷blom
Remove _NOSPAM to reply by mail
 
Reply With Quote
 
 
 
 
Gerbrand van Dieijen
Guest
Posts: n/a
 
      11-26-2004
On Wed, 24 Nov 2004 11:47:22 -0500, Dave Stallard wrote:
>
>for (Iterator i=c.iterator();i.hasNext()
> Object o = i.next(); // You don't need the coercion for Object
>
>Of course, in the new Java you should be able to write something like:
>
>foreach (Object : c)
>


It's not the same, and the first (for (Iterator i) is even incorrect: it
will fail when c is empty. You should always use Iterator it=..;while
(it...) or the new (and much indeed much better) construct for (... :..)
except when you're very sure the collection can never be empty.

--
Gerbrand van Dieijen

 
Reply With Quote
 
Stanimir Stamenkov
Guest
Posts: n/a
 
      11-26-2004
/Gerbrand van Dieijen/:
> On Wed, 24 Nov 2004 11:47:22 -0500, Dave Stallard wrote:
>
>> for (Iterator i=c.iterator();i.hasNext()
>> Object o = i.next(); // You don't need the coercion for Object
>>
>> Of course, in the new Java you should be able to write something like:
>>
>> foreach (Object : c)

>
> It's not the same, and the first (for (Iterator i) is even incorrect: it
> will fail when c is empty.


I fail to see how it would fail if the iterator "is empty"?

> You should always use Iterator it=..;while
> (it...) or the new (and much indeed much better) construct for (... :..)
> except when you're very sure the collection can never be empty.


The 'for' construct has the same effect as with the previous example
- it just hides the Iterator's visibility making it the preferred
for me.

--
Stanimir
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      11-26-2004
Daniel Sj÷blom wrote:

> I created a little test class, and ran it through a little JVMTI agent
> that dumps the JIT-generated native code, and I can confirm that at
> least on the client 1.5.0 JVM on x86 linux, the explicit assignment was
> not removed in the JIT-ed code. That was the only difference between the
> compiled code of the two different approaches.
>
> The server VM on the same JVM did generate equivalent code for both
> approaches. I suspect the client VM does not do any dead code elimination.


Interesting. Thank you.

-- chris


 
Reply With Quote
 
Grant Wagner
Guest
Posts: n/a
 
      11-26-2004
Gerbrand van Dieijen wrote:

> On Wed, 24 Nov 2004 11:47:22 -0500, Dave Stallard wrote:
> >
> >for (Iterator i=c.iterator();i.hasNext()
> > Object o = i.next(); // You don't need the coercion for Object
> >
> >Of course, in the new Java you should be able to write something like:
> >
> >foreach (Object : c)
> >

>
> It's not the same, and the first (for (Iterator i) is even incorrect: it
> will fail when c is empty.


import java.util.*;

public class IteratorTest {
public static void main(String[] s) {

ArrayList<String> c = new ArrayList<String>();

// c.add("Test");

System.out.println(
"The ArrayList is " +
(c.isEmpty() ? "" : "not ") +
"empty"
);

for (Iterator i = c.iterator(); i.hasNext() {
Object o = i.next();
System.out.println(o);
}

// no errors or exceptions, even when c 'isEmpty()'
}
}

--
Grant Wagner <(E-Mail Removed)>

 
Reply With Quote
 
Gerbrand van Dieijen
Guest
Posts: n/a
 
      11-26-2004
On Fri, 26 Nov 2004 16:22:24 GMT, Grant Wagner wrote:
> for (Iterator i = c.iterator(); i.hasNext() {
> Object o = i.next();
> System.out.println(o);
> }
>


Arg, you're right. I'm very ashamed I didn't thought about that, or
figured it out before
--
Gerbrand van Dieijen

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM
ADSL WIC support - which NM's, and which IOS versions? Kralizec Craig Cisco 5 12-08-2005 02:20 AM
How c++ Compiler understands the which is the private func and which is the public fun ???? chetan C++ 5 09-21-2004 08:43 AM
Looking For a tool to crawl ASP and compare to a folder of files to see which are used and which are not. Steve Mauldin ASP General 0 02-06-2004 04:15 PM
Keeping track of which user controls need to be loaded and which not John ASP .Net 0 07-08-2003 09:26 AM



Advertisments