#=======================================================================

__version__ = '''0.0.01'''
__sub_version__ = '''20101129165047'''
__copyright__ = '''(c) Alex A. Naanou 2010'''


#-----------------------------------------------------------------------

import time


#-----------------------------------------------------------------------

class D(dict):
	def __setitem__(self, key, value):
		super(D, self).__setitem__(key, value)
		logger = getattr(self, '__set_logger__', None)
		if logger is not None:
			logger(key, val)

class O(object): pass

def test(obj, count=10000):
	t0 = time.time()
	for i in xrange(count):
		setattr(obj, 'a' + str(i), i)
	t1 = time.time()
	# return: total, time per write
	return t1 - t0, (t1 - t0)/count 

def run(obj, test=test, runs=4, count=10000):
	for c in xrange(runs):
		print test(obj, count)
	print


#-----------------------------------------------------------------------

normal, logged = O(), O()

logged.__dict__ = D()


print 'namespace write for normal object object:'
run(normal)

print 'namespace write for object with hooked namespace:'
run(logged)


#-----------------------------------------------------------------------
#
# The results I get for this are quite interesting:
#
'''
namespace write for normal object object:
(11.868000030517578, 0.0011868000030517579)
(15.312000036239624, 0.0015312000036239624)
(35.73199987411499, 0.0035731999874114989)
(58.916000127792358, 0.0058916000127792354)

namespace write for object with hooked namespace:
(0.089999914169311523, 8.9999914169311522e-006)
(0.039999961853027344, 3.9999961853027345e-006)
(0.039999961853027344, 3.9999961853027345e-006)
(0.039999961853027344, 3.9999961853027345e-006)

'''
# 
# What I expected was that the normal object would show near constant
# times with very little deviation (like it does under CPython).
#
# For reference, here are the times I get for the normal object under
# CPython:
#
'''
namespace write for normal object object:
(0.069999933242797852, 6.999993324279785e-006)
(0.059999942779541016, 5.9999942779541018e-006)
(0.039999961853027344, 3.9999961853027345e-006)
(0.040000200271606445, 4.0000200271606446e-006)

'''
#
# But, what really surprised me at first was that the normal object is 
# slower (three orders of magnitude?!) on ns writes than the hooked. 
# But, I guess, that could be attributed to the new ns structure 
# overhead which is not present in the hooked version, but the
# exponential growth indicates a leak...
#
#
# As can be seen, the hooked run shows roughly the same performance as 
# CPython on my hardware.
#
# NOTE: the hooked version will not work correctly in CPython due to 
#       a "feature" that any access to the namespace falls straight 
#       through the C-API -- I tried to patch it several years back but
#       it did not go through py-dev...
#
#
#=======================================================================
#                                            vim:set ts=4 sw=4 nowrap :
