Re: [picocontainer-dev] ConfigParameterTestCase / ConfigParameter
Jörg Schaible <[email protected]>
| Newsgroups | gmane.comp.java.picocontainer.devel |
|---|---|
| Message-ID | <[email protected]> |
Jörg Schaible wrote:
> Paul Hammant wrote:
[snip]
>> .... can't work because valueOf is not part of an interface/
>> abstraction that safe for generics. You'd have to do that fragment
>> with reflection ... making it uglier and slower.
>
> Seems I have to learn a lot more about Java generics, I did not expect to
> have here reflection involded at all. C++ templates were a wonderful
> mechanism ;-)
However, does it really make such a significant difference?
Converting 1.000.000 Strings into an Integer:
Direct conversion: 0.461
Reflective conversion: 0.505
Running on JDK 5, old AMD Thunderbird 2.4Mhz
- Jörg
public class Benchmark {
public static void main(String[] args) throws SecurityException,
NoSuchMethodException, IllegalArgumentException, IllegalAccessException,
InvocationTargetException {
final int max = 1000000;
final String[] values = new String[max];
for (int i = 0; i < values.length; i++ ) {
values[i] = Integer.toString(i);
}
long now = System.currentTimeMillis();
for (int i = 0; i < values.length; i++ ) {
final Integer iv = Integer.valueOf(values[i]);
if (iv.intValue() != i) {
throw new IllegalStateException("Wrong value at " + i);
}
}
System.out.println("Direct conversion: " +
(System.currentTimeMillis() - now) / 1000.0);
Method method = Integer.class.getMethod("valueOf", new Class[
{String.class});
now = System.currentTimeMillis();
for (int i = 0; i < values.length; i++ ) {
final Integer iv = (Integer)method.invoke(null, values[i]);
if (iv.intValue() != i) {
throw new IllegalStateException("Wrong value at " + i);
}
}
System.out.println("Reflective conversion: " +
(System.currentTimeMillis() - now) / 1000.0);
}
}
---------------------------------------------------------------------
To unsubscribe from this list please visit:
http://xircles.codehaus.org/manage_email