A
Ahmed Moustafa
In a couple of performance articles, it is recommended to dynamically
load classes as needed with Class.forname().
Why?
load classes as needed with Class.forname().
Why?
In a couple of performance articles, it is recommended to dynamically
load classes as needed with Class.forname().
Lothar said:There should be a reason in the articles. IMHO it's better to
initialize instances the "normal" way (using new) to be able
to find errors during compile-time. A
MyClass myClass = Class.forName("Myclass");
will lead to a ClassNotFoundException during runtime. A
MyCLass myClass = new Myclass()
allows the compiler to tell you that there is no class with
name Myclass.
And I'm quite sure that you will lose more time in a
constructor being implemented inefficently that you
will get with using forName instead of new.
Do you have a link to one of these "couple of" articles?
Ahmed Moustafa said:In a couple of performance articles, it is recommended to dynamically
load classes as needed with Class.forname().
Why?
Adam said:I wager that this is a tough test to meet. In my experience, many classes
are loaded long before I naively expect them to be: this is because the JVM
traces static class references in the entire class file as it is being
loaded and recursively loads all of the classes needed to resolve the
symbols. Additionally, the strategy of forcing classes to load with
Class.forName works only when there is a long delay between the time the
forced-load happens (often at startup) and the time the classes are actually
needed. The strategy is fragile: it relies on some list of classes that need
to be pre-loaded; that list needst to be generated and maintained if it is
to be of any use. And since the GC can unload classes if they don't have any
instances or active threads in any method, the strategy is likely to fail to
achieve its purpose anyway.
Classes/Interface
* replace generic standard classes with faster implementations specific to application
* create subclasses to override methods with faster versions
* program using interfaces, so that actual structure can be easily swapped to improve performance ....
* dynamically load class with Class.forname() (why? no reason given)
Both require an array copy.String
* use char[] array directly to create String rather than StringBuffer
* convert string to char[] to process character, rather than using charAt()
pete said:'No reason given'- but how else do you do the first three dynamically?
runs in around 17ms where strings is an array of ten Strings of length 7.
runs in around 5ms with the same data (java 1.4.1_01-39 on OS X)
Jon Skeet said:Out of interest, which VM was that with? It'd be interesting to see the
results on various VMs, as I believe 1.4 is significantly faster at
this than 1.3.1.
Jon said:Hmmm... let me jump in here with no context and just try to hack some
answer...
OK. The first one is easy.
Document myDoc = new MyTunedForMyAppDoc();
JTextArea area = new JTextArea( myDoc ); ....
Or
Document myDoc = new MyStupidButOnly16KDoc();
All three of those can be implemented through several standard patterns,
including factory methods and singletons. No class.forName() required.
So the "optimized" version is more than 3 times slower on your machine?
pete said:Is this dynamic? In this you have replaced the implementation of
Document using a statically linked class. Can the client of your code
choose to replace your implementation of Document with one tuned for
_their_ specific application, or are they stuck with your solution based
on _your_ assumptions as to what to optimise? If this is part a library,
have you enabled the client to apply the three optimisation techniques
listed?
That indeed was the point. Trust the profiler, not the mythology.
In a couple of performance articles, it is recommended to dynamically
load classes as needed with Class.forname().
Why?
Jon said:Sorry. That's what I get for posting when too tired.
You can use those to hide the Class.forName().
One good example is how to get a SAX parser.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.