What method is more efficient?

2 views
Skip to first unread message

Hendré Louw

unread,
Feb 9, 2012, 11:31:47 AM2/9/12
to ctjug...@googlegroups.com
What method is more efficient? 

A

public static ScreenBounds createFromCentroid(ScreenCoordinate centroid, int width, int height) {
int x = centroid.getX();
int y = centroid.getY();
int w = width / 2;
int h = height / 2;
return new ScreenBounds(x - w, y - h, x + w, y + h);
}

B
public static ScreenBounds createFromCentroid(ScreenCoordinate centroid, int width, int height) {
int w = width / 2;
int h = height / 2;
return new ScreenBounds(centroid.getX() - w, centroid.getY() - h, centroid.getX() + w, centroid.getY() + h);
}


Hendré
Blackberry Engineer

David Rubin

unread,
Feb 9, 2012, 11:51:19 AM2/9/12
to ctjug...@googlegroups.com

Hi

I think if you are worried about efficiencies with this level of code you.
a) are working in the wrong language and might want about assembler
b) are calling this in some sort of loop, and might want to try optimize that

 Either way it isn't going to make a difference which one you pick.

But on a side note A is much more readable and I would go for readability over any tiny trivial gain in performance.

David
 

Craig Newton

unread,
Feb 9, 2012, 12:41:12 PM2/9/12
to ctjug...@googlegroups.com
They are pretty much identical, the only extra overhead I see is the two extra integer local variables x and y.

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to CTJUG...@googlegroups.com
To unsubscribe from this group, send email to CTJUG-Forum...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
For jobs see http://jobs.gamatam.com/

Dirk le Roux

unread,
Feb 9, 2012, 1:20:05 PM2/9/12
to ctjug...@googlegroups.com
Hi Hendré,

A will be more efficient on the first few runs, but the hotspot compiler will soon make these two equal. It will be cheaper to allocate the variable upfront (the byte code will do this anyway) than to call the "get" method twice (which will result in one extra line of byte code). This is also assuming the get methods are simple return only methods and not making WS calls to remote servers.

I see your tag says "Blackberry Engineer", I think this code may run faster on android :)

Dirk

Dr Heinz M. Kabutz

unread,
Feb 9, 2012, 1:33:49 PM2/9/12
to ctjug...@googlegroups.com
Well, it depends.  They are not necessarily going to have the same performance.  We are all assuming that getX() and getY() are simple methods that return the fields x and y.  But are you sure of that?  What if getX() does a database lookup?  In that case, A would be faster than B.

But does it matter?

There is an excellent book you should read: http://www.amazon.com/exec/obidos/redirect?link_code=ur2&camp=1789&tag=httpwwwjavasc-20&creative=9325&path=ASIN/1617290068
Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz 

Hendré Louw

unread,
Feb 9, 2012, 1:34:41 PM2/9/12
to ctjug...@googlegroups.com
Thanks for the reply and thanks for the Android tip Dirk.

How can I view the byte code of a specific method?

Dr Heinz M. Kabutz

unread,
Feb 9, 2012, 1:42:39 PM2/9/12
to ctjug...@googlegroups.com
The byte code is irrelevant.

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz 


Hendré Louw

unread,
Feb 9, 2012, 1:50:40 PM2/9/12
to ctjug...@googlegroups.com
Heinz, getX() and getY() return the fields x and y.

Dr Heinz M. Kabutz

unread,
Feb 9, 2012, 2:00:09 PM2/9/12
to ctjug...@googlegroups.com
Right, in which case they are below the 6 bytecode limit at which the method will be extremely aggressively inlined.

The best is to write code that is really easy to read and understand.

How about:


public static ScreenBounds createFromCentroid(ScreenCoordinate centroid, int width, int height) {
  int x0 = centroid.getX() - width/2;
  int x1 = centroid.getX() + width/2;
  int y0 = centroid.getY() - height/2;
  int y1 = centroid.getY() + height/2;
  return new ScreenBounds(x0, y0, x1, y1);
}

Performance is equivalent.  The difference is in readability.  Readability is subjective.  Just because it seems easy to me, might not make it easy to you.  But with this approach, it seems a bit more obvious what you are trying to do.

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz 


Hendré Louw

unread,
Feb 9, 2012, 3:09:56 PM2/9/12
to ctjug...@googlegroups.com
Thank you for the response Heinz.

How can I view the bytecode of a specific line of code?

Dr Heinz M. Kabutz

unread,
Feb 9, 2012, 3:19:21 PM2/9/12
to ctjug...@googlegroups.com
javap -c will show you the bytecode.  However, if you want to view the actual assembler code, use java -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly YourClassName

If you're running this on a mobile device, then you might need to figure out how it is compiled on that device.

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 75 595 262
Skype: kabutz 


Reply all
Reply to author
Forward
0 new messages