• Post Reply Bookmark Topic Watch Topic
  • New Topic

fast deep copy  RSS feed

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

I have to create a deep copy of an object [Space2D instance].



That copying will be critical for the performance of the application.

What is the best way to create a deep copy in a fast way?

Thanks!
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no fast way. The fastest way is to simply iterate over the array (so a nested loop), then duplicating each cell.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lucas Smith wrote:What is the best way to create a deep copy in a fast way?


copy constructor?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also an option. Code similar to mine can be used inside the constructor.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Code similar to mine can be used inside the constructor.

yes, but we can avoid the clone
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried a copy constructor? Note 5 of your 6 fields are primitives, so copying them is very fast.

For the Cell[][] array of arrays, you would have to copy each element of the included arrays, then copy those components into a new array. You can try the clone() method on each component (Cell[]) array, copying them into a new Cell[][] array, or the System#arraycopy(java.lang.Object, int, java.lang.Object, int, int) method. If you use a for loop, remember it won't work in a for-each loop.
If the Cell objets are mutable, you may need to take a copy or clone() of each Cell object, too.

Try the System#nanoTime() method before and after 1000000 copying operations with arraycopy and clone.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you tried a copy constructor?


It works for me

 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.
Rob's suggestion will be good .
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman, that copy constructor doesn't perform a deep copy; the StringBuffer[][] will be shared. Try the following:
Not what you want, is it?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Not what you want, is it?

Thanks rob, you are right! now I get that point.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!