Meaningless Drivel is fun!*
The moose likes Performance and the fly likes Vector Vs Expandable Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Vector Vs Expandable Array" Watch "Vector Vs Expandable Array" New topic
Author

Vector Vs Expandable Array

Eoin Mac Aoidh
Ranch Hand

Joined: May 19, 2005
Posts: 48
Hi,
I am looking for a bit of advice here:
My application has a function that gets the currentTimeMillis() each time the mouseMoved() method is called. So, if the mouse is moved a lot of times, the get currentTimeMillis() method could potentially be called a few thousand times.
At present, Each time the mouseMoved method is called, I do the following:


I want to capture the time as often as possible so I need this part of my code to be as efficient as possile. I am concerned that the creation of a Time object (nothing more than an object to store a long)
each and every time the method is called, is a little slow. - However in order to store it in myVector it has to be done. (I think?!!!)
Would I be better off using an ExpandableArray to save on the cost of object creation? (As I could insert the long "currentTime" directly into the array) What is the trade off in terms of the Array having to continually expand - will this be faster or about the same as using a vector?
I would appreciate any help or tips on this matter.
Thanking you in advance,

Eoin.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
What's an ExpandableArray? No such class in JDK.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
Take a look at the source code for Vector - you will see that it is based on an array that expands (actually a bigger array is created and the contents copied) as needed - also that it does a lot of synchronization that is not needed in all applications which is why ArrayList exists. Note that it is also for object references.

The fastest way to store your timestamp long values would be to build a custom class modeled on ArrayList but based on a long[] array that can be expanded as needed. By storing long primitive values you avoid time consuming object creation and can easily get intervals. Note that a really good guess on the initial array size will be a big help.

Bill
[ February 28, 2007: Message edited by: William Brogden ]
Eoin Mac Aoidh
Ranch Hand

Joined: May 19, 2005
Posts: 48
Thats great Bill, thanks for your time.
I didnt realise that the vector was modelled on an expanding array.
That is a big push in the direction of using a custom class modeled on ArrayList but based on a long[] array.
Thanks again.

E
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You don't even need to write your own - simply use http://jakarta.apache.org/commons/primitives/


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
Before going to any effort of optimizing it I would simply test it with a regular ArrayList. Also, presumably you don't want your data structure to grow unbounded so you would want to put a cap on how big it could get too.


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18872
    
    8

Back-of-the-envelope calculation:

You probably won't get mouse events more often than once per millisecond, and probably not nearly that often.

If you have a 2-gigahertz machine (let's say) then it can execute 2 billion operations in one second. That's 2 million operations in one millisecond. Now clearly it takes more than a few operations to create a Long object and add it to an ArrayList, but I don't think it would take anywhere near 2 million operations.

Computers are fast. You don't have to optimize things nearly as much as you think.
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 861
To clarify pauls point. I added 50,000,000 elements to the arraylist in the following code on my pc in 3.5 seconds.



Using the following method (which is pretty much what you are asking to do) I could add about 2,000,000 new Long objects to the ArrayList per second (50,000,000 took 25 seconds).

As paul said this would easily easily keep up with a mouse. You could speed this up, but i doubt it will be your applications bottleneck.

You should write simple performance tests like these to test any performance theories you may have.

[ February 28, 2007: Message edited by: steve souza ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Vector Vs Expandable Array