96 lines
2.2 KiB
Plaintext
96 lines
2.2 KiB
Plaintext
|
From: atai-no-spam at glink.net.hk (>0 Remove -no-spam to reply)
|
||
|
Date: 29 Apr 1999 11:42:16 GMT
|
||
|
Subject: Shelve vs. Deepcopy
|
||
|
Message-ID: <slrn7igh7r.h49.atai-no-spam@aaa.tai.org>
|
||
|
Content-Length: 2001
|
||
|
X-UID: 258
|
||
|
|
||
|
Hi:
|
||
|
I have a class who contains reference to other classes. Now I would like
|
||
|
to test an instance of this class. The test would alter the instance but I
|
||
|
would like to retain this instance, so at first I make a deepcopy of this
|
||
|
instance and perform the test on the copy. However, it take quite a long
|
||
|
time to finish the test. Then I try to use shelve to do the same thing.
|
||
|
I.e. first store the instance on a dbm. Then the copy is obtain from
|
||
|
loading the dbm. And to my surprise, the latter method is much faster than
|
||
|
the first method. With some profiling, I saw that the number of functions
|
||
|
calls of the first method is at least twice that of the second one. The
|
||
|
following are two test programs to illustrate this. When the first one is
|
||
|
run, using shelve is faster. While in the second one, deepcopy is faster.
|
||
|
I hope I have make some sense in the above sentences.
|
||
|
Regards,
|
||
|
>0
|
||
|
|
||
|
|
||
|
test.py
|
||
|
Cut here -----------------------------------------------
|
||
|
|
||
|
import copy, shelve, time
|
||
|
|
||
|
class test:
|
||
|
def __init__(self, num):
|
||
|
self.a = num
|
||
|
self.b = (2,3,`num`)
|
||
|
self.c = (3,7, `num`)
|
||
|
class test1:
|
||
|
def __init__(self, obj):
|
||
|
self.dict = {(1,3):obj}
|
||
|
for i in range(100):
|
||
|
self.__dict__[(`i`,1)] = i
|
||
|
|
||
|
a = test(7)
|
||
|
|
||
|
b = test1(a)
|
||
|
|
||
|
dbase = shelve.open('tmp')
|
||
|
dbase['store'] = b
|
||
|
|
||
|
start = time.time()
|
||
|
for i in range(1000):
|
||
|
c = dbase['store']
|
||
|
stop = time.time()
|
||
|
print stop - start
|
||
|
|
||
|
start = time.time()
|
||
|
for i in range(1000):
|
||
|
c = copy.deepcopy(b)
|
||
|
stop = time.time()
|
||
|
print stop - start
|
||
|
|
||
|
dbase.close()
|
||
|
|
||
|
|
||
|
test2.py
|
||
|
Cut here-------------------------------------------------
|
||
|
|
||
|
|
||
|
import copy, shelve, time
|
||
|
|
||
|
class test:
|
||
|
def __init__(self):
|
||
|
for i in range(100):
|
||
|
self.__dict__[`i`] = i
|
||
|
|
||
|
a = test()
|
||
|
|
||
|
dbase = shelve.open('tmp')
|
||
|
dbase['store'] = a
|
||
|
|
||
|
start = time.time()
|
||
|
for i in range(1000):
|
||
|
c = dbase['store']
|
||
|
stop = time.time()
|
||
|
print stop - start
|
||
|
|
||
|
start = time.time()
|
||
|
for i in range(1000):
|
||
|
c = copy.deepcopy(a)
|
||
|
stop = time.time()
|
||
|
print stop - start
|
||
|
|
||
|
dbase.close()
|
||
|
|
||
|
|
||
|
|
||
|
|