from sys import stderr
from time import time
from copy import deepcopy
import gc

# set to test time instantiation of a list takes in case of the 
# garbage collector gets invoked (set to -1 to disable)
#
# Python version (it seems that the loop count depends from the platform):
# Python 2.4.1 (#1, May 27 2005, 18:02:40) 
# [GCC 3.3.3 (cygwin special)] on cygwin
generation_to_test = 2
loopCount = (87895, 88595, 89293, 89000)[generation_to_test]

# set to True to let the collector collect
# set to False to only trigger collector without causing it to collect
make_unreachable = True

aList = []
prep = []
circular = []
circular.append(circular)

#gc.set_debug(gc.DEBUG_STATS)

for i in xrange(loopCount):
    prep.append(deepcopy(circular))
    aList.append(None)

gc.set_debug(gc.DEBUG_STATS)

#print "\nfilling prepared aList ..."
for i in xrange(loopCount):
    aList[i] = prep[i]
#    aList.append(prep[i])

if 0:
    print "\nexplicit collect ..."
    t = time()
    gc.collect()
    print time() - t

print "\ndeleting lists ..."
t = time()
del prep
if make_unreachable:
    del aList

print "\ninstantiating a list ..."
print " - generation_to_test = %s" % generation_to_test
print " - make_unrachable = %s" % make_unreachable
t = time()
list1 = [circular]
list2 = [circular]
list3 = [circular]
list4 = [circular]
list5 = [circular]
list6 = [circular]
list7 = [circular]
list8 = [circular]
list9 = [circular]
list10 = [circular]
list11 = [circular]
list12 = [circular]
list13 = [circular]
list14 = [circular]
list15 = [circular]
list16 = [circular]
list17 = [circular]
list18 = [circular]
list19 = [circular]
print time() - t

gc.set_debug(0)

#print "\nexplicit collect ..."
t = time()
gc.collect()
#print time() - t

#print "\ncollect before interpreter exits..."

# ---------------------------------
# results as of 04.01.2006
# ---------------------------------

results = """
weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = -1
 - make_unrachable = False
0.0

weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = 0
 - make_unrachable = False
gc: collecting generation 0...
gc: objects in each generation: 702 6990 83264
gc: done.
0.000999927520752

weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = 1
 - make_unrachable = False
gc: collecting generation 1...
gc: objects in each generation: 702 7689 83264
gc: done.
0.00300002098083

weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = 2
 - make_unrachable = False
gc: collecting generation 2...
gc: objects in each generation: 702 0 91652
gc: done.
0.0179998874664




weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = -1
 - make_unrachable = True
0.0

weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = 0
 - make_unrachable = True
gc: collecting generation 0...
gc: objects in each generation: 703 6990 83263
gc: done, 696 unreachable, 0 uncollectable.
0.00200009346008

weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = 1
 - make_unrachable = True
gc: collecting generation 1...
gc: objects in each generation: 703 7689 83263
gc: done, 8386 unreachable, 0 uncollectable.
0.00699996948242

weber@stahl ~/Projects/ICS/ServerRevC/notes
$ python gctest.py

deleting lists ...

instantiating a list ...
 - generation_to_test = 2
 - make_unrachable = True
gc: collecting generation 2...
gc: objects in each generation: 703 0 91651
gc: done, 89293 unreachable, 0 uncollectable.
0.0650000572205
"""