Re: Template Emulation?
- From: Eric Sosman <Eric.Sosman@xxxxxxx>
- Date: Wed, 11 Apr 2007 16:41:39 -0400
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
.
- References:
- Template Emulation?
- From: christoph . ortner
- Template Emulation?
- Prev by Date: Re: "doubts" != "questions" (was: Dynamic Loading of Classes)
- Next by Date: load properties file with comments
- Previous by thread: Template Emulation?
- Next by thread: Re: Template Emulation?
- Index(es):
Relevant Pages
|