Chinaunix首页 | 论坛 | 博客
  • 博客访问: 463353
  • 博文数量: 108
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 1134
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-29 19:43
文章分类

全部博文(108)

文章存档

2016年(10)

2015年(9)

2014年(73)

2013年(16)

我的朋友

分类: Python/Ruby

2014-07-31 11:10:40


点击(此处)折叠或打开

  1. [root@cdn ~]# python -m timeit '"_".join(str(n) for n in range(100))'
  2. 10000 loops, best of 3: 37.5 usec per loop
  3. [root@cdn ~]# python -m timeit '"_".join([str(n) for n in range(100)])'
  4. 10000 loops, best of 3: 32.6 usec per loop
  5. [root@cdn ~]# python -m timeit '"_".join(map(str,range(100)))'
  6. 10000 loops, best of 3: 22.2 usec per loop
  7. [root@cdn ~]# python -m timeit '"_".join([str(n) for n in xrange(100)])'
  8. 10000 loops, best of 3: 31.8 usec per loop

点击(此处)折叠或打开

  1. python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
  2. Where the following options are understood:
  3. -n N, --number=N
  4. how many times to execute ‘statement’
  5. -r N, --repeat=N
  6. how many times to repeat the timer (default 3)
  7. -s S, --setup=S
  8. statement to be executed once initially (default pass)
  9. -t, --time
  10. use time.time() (default on all platforms but Windows)
  11. -c, --clock
  12. use time.clock() (default on Windows)
  13. -v, --verbose
  14. print raw timing results; repeat for more digits precision
  15. -h, --help
  16. print a short usage message and exit

点击(此处)折叠或打开

  1. [root@cdn ~]# python
  2. Python 2.6.6 (r266:84292, Jan 21 2013, 17:47:21)
  3. [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> import timeit
  6. >>> timeit.timeit('char in text' ,setup='text= "sample string";char = "g"')
  7. 0.10500788688659668
  8. >>> timeit.timeit('text.find(char)' ,setup='text= "sample string";char = "g"')
  9. 0.24353289604187012
  10. >>> text= "sample string";char = "g"
  11. >>> text.find(char)
  12. 12


点击(此处)折叠或打开

  1. >>> t=timeit.Timer('char in text', setup=' text=" sample string"; char = "g"')
  2. Traceback (most recent call last):
  3. File "", line 1, in
  4. File "/usr/local/lib/python2.6/timeit.py", line 136, in __init__
  5. code = compile(src, dummy_src_name, "exec")
  6. File "", line 4
  7. _t0 = _timer()
  8. ^
  9. IndentationError: unindent does not match any outer indentation level
  10. ###注意 setup里的字符串不能有空格
  11. >>> t=timeit.Timer('char in text', setup='text=" sample string"; char = "g"')
  12. >>> t.timeit()
  13. 0.10593914985656738
  14. >>> t.repeat()
  15. [0.10739803314208984, 0.10854411125183105, 0.10851407051086426]
  16. >>>
python中使用try/exception

点击(此处)折叠或打开

  1. [root@cdn ~]# python -m timeit 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass'
  2. 1000000 loops, best of 3: 1.71 usec per loop
  3. [root@cdn ~]# python -m timeit 'if hasattr(str, "__nonzero__"): pass'
  4. 1000000 loops, best of 3: 0.836 usec per loop
  5. [root@cdn ~]# python -m timeit 'try:' ' int.__nonzero__' 'except AttributeError:' ' pass'
  6. 10000000 loops, best of 3: 0.112 usec per loop
  7. [root@cdn ~]# python -m timeit 'if hasattr(int, "__nonzero__"): pass'
  8. 1000000 loops, best of 3: 0.209 usec per loop

timeit访问自定义函数,使用setup参数。

点击(此处)折叠或打开

  1. def test():
  2.     L=[]
  3.     for i in range(100):
  4.         L.append(i)

  5. if __name__ == '__main__':
  6.     import timeit
  7.     print(timeit.timeit("test()",setup="from __main__ import test"))



阅读(1679) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~