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,
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 ]
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.
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
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.
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.
Joined: Jun 26, 2002
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 ]