python - copy.deepcopy or create a new object? -


i'm developing real-time application , need create instances new objects same data.

first, did instantiating them, realised maybe copy.deepcopy faster. now, find people deepcopy horribly slow.

i can't copy.copy because object has lists.

my question is, know faster way or need give , instantiate them again? thank time

i believe copy.deepcopy() still pure python, it's unlikely give speed boost.

it sounds me little classic case of optimisation. suggest writing code intuitive which, in opinion, instantiating each object. can profile , see savings need made, if anywhere. may in real-world use-case different piece of code bottleneck.

edit: one thing forgot mention in original answer - if you're copying list, make sure use slice notation (new_list = old_list[:]) rather iterating through in python, slower. won't deep copy, however, if lists have other lists or dictionaries you'll need use deepcopy(). dict objects, use copy() method.

if still find constructing objects what's taking time, can consider how speed up. experiment __slots__, although they're typically saving memory more cpu time doubt they'll buy much. in extreme case, can push object out c extension module, lot faster @ expense of increased complexity. approach i've taken in past, use native c data structures under hood , use python's special methods wrap "list-like" or "dict-like" interface on top. rely on being happy coding in c, of course.

(as aside avoid c++ unless have compelling reason, c++ python extensions more fiddly building plain c - it's entirely possible if have motivation, however)

if object has, example, long lists might mileage out of sort of copy-on-write approach, clones of objects keep same references instead of copying lists. every time access them use sys.getrefcount() see if it's safe update in-place or whether need take copy. approach error-prone , overly complex, thought i'd mention interest.

you @ object hierarchy , see whether can break objects such parts don't need duplicated can shared between other objects. again, you'd need take care when modifying such shared objects.

the important point, however, first want code correct , then make code fast once understand best ways real world usage.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -