Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Vector Vs Expandable Array

 
Eoin Mac Aoidh
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's an ExpandableArray? No such class in JDK.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't even need to write your own - simply use http://jakarta.apache.org/commons/primitives/
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic