Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Template Emulation?

Thread view: 
christoph.ortner@comlab.ox.ac.uk - 11 Apr 2007 20:53 GMT
Hi everyone,

I have some performance critical code and was wondering whether there is
some way to emulate C++ templates in order to tell the Java compiler to
optimize it somehow.
The following is a much simplified example, but the principle is the
same. Consider a class
class Vec {

 public Vec(int dim) {
   this.dim = dim;
   this.x = new double[dim];
 }

 private int dim;
 public double x[];

 public void axpy(double alpha, Vec b) {
   for (int i = 0; i < dim; i++)
     this.x[i] += alpha * b.x[i];
 }

}

Suppose now that the dimension is defined once in the program (possibly
as a static final variable) and then all instances of Vec have the same
dimension (say dim = 3). In this case, there should be some way for the
compiler to understand this, and unroll and inline the axpy() function.
Is there any way to achieve this in Java?

What if I declared dim as static final in the class Vec? If I would
hard-code the dimension so-to-speak?
Many thanks,
Christoph
Stefan Ram - 11 Apr 2007 21:25 GMT
christoph.ortner@comlab.ox.ac.uk did not write:
|class Vec
|{ public Vec( final int dim ){ this.dim = dim; this.x = new double[ dim ]; }
|  public void add( final double alpha, final Vec b )
|  { for( int i = 0; i < dim; ++i )this.x[ i ]+= alpha * b.x[ i ]; }
|  final private int dim; public double x[]; }
|Is there any way to inline »add« this in Java?

 No - Java does not have inlining.

 Specific Java implementations might have. See for example the
 recent discussion

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103956
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6533165

 These discussions also provide hints about making inlining happen.

 See also

http://java.sun.com/docs/hotspot/HotSpotFAQ.html

 But do not invest any work unless you have /measured/
 the most obvious straight-forward readable implementation
 to be too slow.

 Be aware that any optimization might result in worse
 performance on any other platform and version than the one
 optimized for, might introduce bugs or raise maintenance
 costs.

>emulate C++ templates

 Hotspot already is quite smart and can outperform C++ in
 some cases because it uses information obtained at runtime.

 Fixing »dim« to a compile-time constant might have no or only
 a very small influence on the speed of the execution of the
 methods. You might like to measure this in the target
 environment.
Stefan Ram - 11 Apr 2007 21:27 GMT
Supersedes: <optimization-20070411221157@ram.dialup.fu-berlin.de>

christoph.ortner@comlab.ox.ac.uk did not write:
|class Vec
|{ public Vec( final int dim ){ this.dim = dim; this.x = new double[ dim ]; }
|  public void add( final double alpha, final Vec b )
|  { for( int i = 0; i < dim; ++i )this.x[ i ]+= alpha * b.x[ i ]; }
|  final private int dim; public double x[]; }
|Is there any way to inline »add« in Java?

 No - Java does not have inlining.

 Specific Java implementations might have. See for example the
 recent discussion

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5103956
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6533165

 These discussions also provide hints about making inlining happen.

 See also

http://java.sun.com/docs/hotspot/HotSpotFAQ.html

 But do not invest any work unless you have /measured/
 the most obvious straight-forward readable implementation
 to be too slow.

 Be aware that any optimization might result in worse
 performance on any other platform and version than the one
 optimized for, might introduce bugs or raise maintenance
 costs.

>emulate C++ templates

 Hotspot already is quite smart and can outperform C++ in
 some cases because it uses information obtained at runtime.

 Fixing »dim« to a compile-time constant might have no or only
 a very small influence on the speed of the execution of the
 methods. You might like to measure this in the target
 environment.

Supersedes: <optimization-20070411221157@ram.dialup.fu-berlin.de>
Eric Sosman - 11 Apr 2007 21:41 GMT
christoph.ortner@comlab.ox.ac.uk wrote On 04/11/07 15:53,:

> Hi everyone,
>
[quoted text clipped - 28 lines]
> What if I declared dim as static final in the class Vec? If I would
> hard-code the dimension so-to-speak?

   First, see Stefan Ram's response.  His principal message
is that the only way to tell for sure is to try it both ways
and measure, to which I'd add that micro-benchmarks in Java
are *very* tricky.

   Second, the `dim' element seems pointless since Java
arrays already know their lengths: it merely duplicates the
value `x.length'.  Whether eliinating `dim' in favor of
`x.length' would be faster or slower is a matter for yet
another measurement.

Signature

Eric.Sosman@sun.com

Daniel Pitts - 12 Apr 2007 00:42 GMT
On Apr 11, 12:53 pm, christoph.ort...@comlab.ox.ac.uk wrote:
> Hi everyone,
>
[quoted text clipped - 30 lines]
> Many thanks,
> Christoph

Are you sure the particular snippet you are optimizing is really as
performance critical as you believe?  Often the only way to determine
this is by using a profiler.  The only way to use a profiler is to run
working code.  The only way to run working code is to have written
working code.

I suggest you work on creating a well-designed working project before
you decide to complicate your design to optimize an unprofiled
application.  The laws of profiling are: 1. Don't do it, and 2. (For
experts only) Don't do it yet.

Follow these laws and your life will be *so* much easier.  Let the
profiler tell you whats slow, not your gut.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.