More Java posts
January 17, 2002
Is myObject.clone() faster than myClass.newInstance()?

What the program has to do is create objects based on the data it reads. So, it has a HashMap keyed by the names of the possible tokens that can be read. Two possible ways to create the objects are

  1. Have the HashMap values be the classes to be instantiated and use .newInstance
  2. Have the HashMap values be prototypical instances of the classes and use .clone()

And the answer is that for my testing the whole effect is just about swamped by whatever else the program is doing. Actual figures below.

Here are four runs of my XMLReader implementation using .newInstance to create objects.

20:12:14.617 - 20:10:43.096 = 91.521
20:14:52.906 - 20:13:22.079 = 90.827
20:17:23.842 - 20:15:52.973 = 90.869
20:19:43.268 - 20:18:09.606 = 93.662

And here are four runs where .clone is used.

21:24:52.506 - 21:23:22.309 = 90.197
21:27:49.826 - 21:26:19.439 = 90.387
21:30:17.337 - 21:28:46.900 = 90.437
21:32:47.025 - 21:31:16.389 = 90.636
Posted by Alex at January 17, 2002 10:06 PM
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?