- 论坛徽章:
- 0
|
- #!/usr/bin/python
- import time
- import sys
- class dct(object):
- def __init__(self, d):
- self.__d = d
- def __getattr__(self, k):
- try:
- result = self.__d[k]
- if isinstance(result, dict):
- o = dct(result)
- setattr(self, k, o)
- return o
- else:
- return result
- except:
- raise AttributeError
- class Meta(type):
- def __getattribute__(self,name):
- _attr=object.__getattribute__(self,name)
- if isinstance(_attr,dict):
- setattr(self,name,Meta('',(object,),_attr))
- _attr=object.__getattribute__(self,name)
- return _attr
- d={'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':
- {'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':
- {'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':
- {'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':
- {'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':{'c':
- {'c':{'c':{'c':{'c':{'a':None,'b':None,'c':None}
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
- if sys.argv[1] == 'shhgs':
- foo = dct(d)
- elif sys.argv[1] == 'luff':
- foo = Meta('foo', (object, ), d)
- else:
- sys.exit(1)
- start=time.time()
- n=10000
- while n>0:
- foo.c
- foo.c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.a
- foo.c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.b
- foo.c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c\
- .c.c.c.c.c.c.c.c.c.c
- n-=1
- print time.time()-start
复制代码- $./a.py shhgs
- 0.318892002106
- $./a.py luff
- 7.18495512009
复制代码 |
|