Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > performance of Class.forname()?

Reply
Thread Tools

performance of Class.forname()?

 
 
Peter McDonnell
Guest
Posts: n/a
 
      07-22-2003
"I substituted a factory class for the dynamic loader and got a 100%
increase in the parsing part of the descriptor loader."

Not sure what you're measuring here. If you don't include the time taken to
statically load the referenced classes (presumeably as part of the
class-loading of the parser itself), then you are only comparing dynamically
loading classes vs having them loaded already, which isn't really a fair
comparison.

As mentioned earlier the time taken to statically load the classes may
appear much earlier in your program load time, but will still count against
overall performance.

You could try pre-loading the Class objects into a Map before you started
parsing and get a better comparison over the newInstance() vs new Classname
performance though. Otherwise I think you'd have to measure the performance
gain over the whole run-time of the program (or at least up until the
parsing has completed) to get a more accurate static vs dynamic loading
comparison.

Regards,
Peter

"Chris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> This might not be exactly pertinent to loading of classes, but it's a
> related issue...
>
> One of my current projects has an XML descriptor that defines how the
> various objects in my data model are connected.
>
> i.e.
>
> <thing class="a.b.c.whatever" ...>
>
> Originally I had the classes for these objects loaded/created using
> Class.forName() and newInstance().
>
> When I came to optimising the software, I found that the loader was
> quite slow, even after discounting the overhead of the XML
> loader/parser.
>
> I substituted a factory class for the dynamic loader and got a 100%
> increase in the parsing part of the descriptor loader.
>
> i.e.
>
> if( type.equals( "a" ) )
> {
> return new A( ... )
> }
> else
> if( type.equals( "b" ) )
> {
> return new B( ... )
> }
> etc
>
> So even with the overhead of numerous string comparisons the factory
> method was twice as fast.
>
> I only has a dozen or so classes, but I dummied up a 100 string
> comparisons and it was still faster than dynamic loading and
> newInstance().
>
> - sarge
>



 
Reply With Quote
 
 
 
 
pete kirkham
Guest
Posts: n/a
 
      07-22-2003
Jon A. Cruz wrote:

> pete kirkham wrote:
>
>>> http://www.cs.utexas.edu/users/toktb...s.html#classes
>>>

>>
>>
>>
>> includes:
>> > 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)

>>
>>
>>
>> 'No reason given'- but how else do you do the first three dynamically?
>>

>
> 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();


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?

> All three of those can be implemented through several standard patterns,
> including factory methods and singletons. No class.forName() required.


How do you implement these patterns dynamically, by which I mean
allowing the client to replace the default implementation with one
optimised for their application, without using Class.forName() (or
hacking bytecode/calling the compiler etc.)?

> So the "optimized" version is more than 3 times slower on your machine?


That indeed was the point. Trust the profiler, not the mythology.


Pete

 
Reply With Quote
 
 
 
 
Jon A. Cruz
Guest
Posts: n/a
 
      07-23-2003
pete kirkham wrote:
> 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?


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.


>
>
> That indeed was the point. Trust the profiler, not the mythology.
>


And remember, it might differ on each system. What you do to help on one
can hurt on another.

 
Reply With Quote
 
dhek bhun kho
Guest
Posts: n/a
 
      07-27-2003
Hello Ahmed

Ahmed Moustafa <(E-Mail Removed)>, Sun, 20 Jul 2003 03:42:05 +0000:

> In a couple of performance articles, it is recommended to dynamically
> load classes as needed with Class.forname().
>
> Why?


It's very dependent on a lot of factors, just like most of the other
optimisations. There aren't many hard and fast rules. For one thing, I
would not trust an article that is date around the release data of java
1.2. There have been a lot of changes in the JVM itself since then.

The Class.forName() argument probably holds for the user, when several
things like 1) The application code is HUGE, and 2) The code is loaded over
the network, are true.

If the code base is huge, and you start a program, you must remember that
you have a programmed a big class tree. So when you load the first class,
the class loader will try to load all classes referenced by the first call
and link them.

If everything is linked statically, then the JVM will load
and link every single class of your program before your application even
starts. That can be perceived as a huge performance hit, because the
application takes longer to start. (I'm almost sure this is the case with
Swing applets).

If you can divide the application in several parts where
certain parts are started by an object that is instantiated by a class
loaded with Class.forName, everything starting at that class is not loaded
until the code actually executes the Class.forName call. Combine that fact
with low-speed connections and you have to wait a long time. There may be
other issues that aren't so apparent.

Greets
Bhun.


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      07-29-2003
Jon A. Cruz wrote:
> pete kirkham wrote:
> > 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?

>
> 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.


No, no, no! Jon, you were right before. Class.forName is not required
to provide flexibility to clients in tuning performance. In fact, a far
better way to do this in the general case is to define interfaces and
let clients specify the correct behavior in code by providing an
implementation of that interface. If new objects need to be created,
passing in factory interfaces can do the trick.

Class.forName comes in when you want to be able to modify the
configuration of an application without making code changes. That's
really a somewhat rare need; it's usefulness has largely been
manufactured by Sun. By definition in the case cited above, it's a Java
programmer that wrote that code, and it's a Java programmer that wants
to use it, so the link can be made in Java, just as easily as in some
properties file in a META-INF directory.

I've long had this vision of Sun sitting around thinking "what does Java
have that other popular languages don't? Oh, reflection! Let's start
pushing it like it's the solution to all the world's problems!", and so
was the nightmare of J2EE born. It's rather unfortunate -- nay, tragic
-- that this has now spread into J2SE classes like a hideous cancer!

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance Tutorials Services - Boosting Performance by DisablingUnnecessary Services on Windows XP Home Edition Software Engineer Javascript 0 06-10-2011 02:18 AM
OCZ Rally High Performance USB2 (Dual Channel) Flash Memory Silverstrand Front Page News 0 09-20-2005 03:35 AM
How to quickly improve your computer's performance @ Bona... Silverstrand Front Page News 0 08-24-2005 01:26 PM
HEXUS.review: Rockdirect XTI 3.8 Performance Silverstrand Front Page News 0 08-08-2005 02:37 PM
Web Form Performance Versus Single File Performance jm ASP .Net 1 12-12-2003 11:14 PM



Advertisments