aspose file tools*
The moose likes Performance and the fly likes add and increment array elements performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "add and increment array elements performance" Watch "add and increment array elements performance" New topic
Author

add and increment array elements performance

Moulay Driss
Greenhorn

Joined: May 05, 2013
Posts: 5
(x,y).

T = (1,1) (2,2) (3,3) (4,4)

I want to increment the value of the variable **x** of object each time a new element is added to array **with the faster way possible** . The new element can be added in any position and we increment all **x** element after the insertion position (position +1)

Before add (6,6) :

`T = (1,1) (2,2) (3,3) (4,4)`

After add (6,6) in different positions:

1) T = (6,6) (2,1) (3,2) (4,3) (5,4)

or

2) T = (1,1) (2,2) (6,6) (4,3) (5,4)

or

3) T = (1,1) (2,2) (3,3) (6,6) (5,4)



I used the method arraycopy to add the new element, and loop for to increment the variable x for each element, as follow:


1. increment all `x` of object elements with loop for

2. `Ta[0] = (6,6)`

3. `araycopy(T, 0, Ta, 1, T.size-1 );`


because it is faster than

While (i< T.length){

T[i] = T[i+1]

T[i].x ++;

i++;
}

I need to add the new element and increment the other objects of array simultaneously with a faster time.


//-------------------


//----------------



//------- The results:

Time without Incrementation : 15 ms

Time with Incrementation: 156 ms


My objective is to minimize as possible the time of incrementation process

(Time with Incrementation < Time without Incrementation * 2 )

because actually

Time with Incrementation (156 ms) = Time without Incrementation (15 ms )* 10

I notes that i can added a new element in any position, but i chose the worst case (adding an element in the first position that requires the incrementation of all x element of the arraylist)
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Unless this is a homework assignment, consider using a data structure that isn't an array/arraylist. A map would let you have constant access by key.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

you'll usually server yourself better by writing code that is easy to follow than uses every possible trick to save time. If you use enough tricks and save yourself a microsecond each time, but spend and extra eight hours debugging this at some point int the future, your code is going to have to run a LOT to make up that lost time.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Moulay Driss
Greenhorn

Joined: May 05, 2013
Posts: 5

suppose an array of object: Element T[ ] , each object contains two integer variables (x,y).

T = (1,1) (2,2) (3,3) (4,4)

I want to increment the value of the variable **x** of object each time a new element is added to array **with the faster way possible** . The new element can be added in any position and we increment all **x** element after the insertion position (position +1)

Before add (6,6) :

`T = (1,1) (2,2) (3,3) (4,4)`

After add (6,6) in different positions:

1) T = (6,6) (2,1) (3,2) (4,3) (5,4)

or

2) T = (1,1) (2,2) (6,6) (4,3) (5,4)

or

3) T = (1,1) (2,2) (3,3) (6,6) (5,4)



I used the method arraycopy to add the new element, and loop for to increment the variable x for each element, as follow:


1. increment all `x` of object elements with loop for

2. `Ta[0] = (6,6)`

3. `araycopy(T, 0, Ta, 1, T.size-1 );`


because it is faster than

While (i< T.length){

T[i] = T[i+1]

T[i].x ++;

i++;
}

I need to add the new element and increment the other objects of array simultaneously with a faster time.


//-------------------


//----------------



//------- The results:

Time without Incrementation : 15 ms

Time with Incrementation: 156 ms


My objective is to minimize as possible the time of incrementation process

(Time with Incrementation < Time without Incrementation * 2 )

because actually

Time with Incrementation (156 ms) = Time without Incrementation (15 ms )* 10

I notes that i can added a new element in any position, but i chose the worst case (adding an element in the first position that requires the incrementation of all x element of the arraylist)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11314
    
  16

Moulay Driss wrote:My objective is to minimize as possible the time of incrementation process

That's not a very well-defined spec, but if it is true, you will quite probably get the most bang for your buck if you buy a faster server. That would have the added bonus of speeding up EVERYTHING on the box, not just this one tiny piece.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: add and increment array elements performance