Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 361853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:11:20+00:00 2026-05-12T13:11:20+00:00

I have a tree structure of widgets e.g. collection contains models and model contains

  • 0

I have a tree structure of widgets e.g. collection contains models and model contains widgets. I want to copy whole collection, copy.deepcopy is faster in comparison to ‘pickle and de-pickle’ing the object but cPickle as being written in C is much faster, so

  1. Why shouldn’t I(we) always be using cPickle instead of deepcopy?
  2. Is there any other copy alternative? because pickle is slower then deepcopy but cPickle is faster, so may be a C implementation of deepcopy will be the winner

Sample test code:

import copy
import pickle
import cPickle

class A(object): pass

d = {}
for i in range(1000):
    d[i] = A()

def copy1():
    return copy.deepcopy(d)

def copy2():
    return pickle.loads(pickle.dumps(d, -1))

def copy3():
    return cPickle.loads(cPickle.dumps(d, -1))

Timings:

>python -m timeit -s "import c" "c.copy1()"
10 loops, best of 3: 46.3 msec per loop

>python -m timeit -s "import c" "c.copy2()"
10 loops, best of 3: 93.3 msec per loop

>python -m timeit -s "import c" "c.copy3()"
100 loops, best of 3: 17.1 msec per loop
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:11:20+00:00Added an answer on May 12, 2026 at 1:11 pm

    Problem is, pickle+unpickle can be faster (in the C implementation) because it’s less general than deepcopy: many objects can be deepcopied but not pickled. Suppose for example that your class A were changed to…:

    class A(object):
      class B(object): pass
      def __init__(self): self.b = self.B()
    

    now, copy1 still works fine (A’s complexity slows it downs but absolutely doesn’t stop it); copy2 and copy3 break, the end of the stack trace says…:

      File "./c.py", line 20, in copy3
        return cPickle.loads(cPickle.dumps(d, -1))
    PicklingError: Can't pickle <class 'c.B'>: attribute lookup c.B failed
    

    I.e., pickling always assumes that classes and functions are top-level entities in their modules, and so pickles them “by name” — deepcopying makes absolutely no such assumptions.

    So if you have a situation where speed of “somewhat deep-copying” is absolutely crucial, every millisecond matters, AND you want to take advantage of special limitations that you KNOW apply to the objects you’re duplicating, such as those that make pickling applicable, or ones favoring other forms yet of serializations and other shortcuts, by all means go ahead – but if you do you MUST be aware that you’re constraining your system to live by those limitations forevermore, and document that design decision very clearly and explicitly for the benefit of future maintainers.

    For the NORMAL case, where you want generality, use deepcopy!-)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a tree structure UL --LI ---INPUT (checkbox) And I want to
I have a tree structure and I want to find all nodes matching a
I have an arbitrary tree structure of nodes. I want to draw this tree
I want to create tree structure using web service. I have used bottom up
I have a tree structure in memory that I would like to render in
I have a tree data structure that is L levels deep each node has
I have a sort of tree structure that represent a hierarchy of layers in
I have a table in my database which stores a tree structure. Here are
I have a table in SQL server that has the normal tree structure of
Let's say I have a recursive data structure class Tree { private Tree right;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.