Re: Template Emulation?



christoph.ortner@xxxxxxxxxxxxxxx wrote On 04/11/07 15:53,:

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?

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.

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Template Emulation?
    ... some way to emulate C++ templates in order to tell the Java compiler to ... public Vec(int dim) { ... Suppose now that the dimension is defined once in the program (possibly ... What if I declared dim as static final in the class Vec? ...
    (comp.lang.java.programmer)
  • OWA Calendarview script error.
    ... There is a system of Java functions that is responsible ... XML returned by the xmlhttp.responseXML.xml object because ... Dim xmlDoc As New MSXML2.DOMDocument30 ... >the calendar folder, ...
    (microsoft.public.exchange.connectivity)
  • Re: Template Emulation?
    ... public Vec(int dim) { ... What if I declared dim as static final in the class Vec? ... The only way to use a profiler is to run ... The only way to run working code is to have written ...
    (comp.lang.java.programmer)
  • Re: Calling J2EE web service from .NET
    ... When I run my Java test, according to netmon, the HTTP header of the request ... contains a "Basic Authentication" section with the realm containing a UserId ... Dim oper As New WSRef.myOper ... I created a Java ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Rollover button problem
    ... | I'm building a site in Dreamweaver MX where I have a set of rollover ... These buttons go dim when the mouse is over them. ... | understanding Java script code so let me know what bits might help ... You have posted to a java newsgroup. ...
    (comp.lang.java.softwaretools)