• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

fast deep copy

 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 77939
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.
Rob's suggestion will be good .
 
Rob Spoor
Sheriff
Posts: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22743
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic