import datetime def time_1(): begin = datetime.datetime.now() sum = 0 for i in xrange(10000000): sum = sum + i end = datetime.datetime.now() return end-begin print time_1()
In [5]: import timeit
In [6]: timeit.timeit("sum(range(100))")
Out[6]: 1.2272648811340332
➜ Python python -m timeit -s"import time_1 as t" "t.time_1()" 0:00:00.282044 10 loops, best of 3: 279 msec per loop
import datetime
def time_1():
begin = datetime.datetime.now()
sum = 0
for i in xrange(10000000):
sum = sum + i
end = datetime.datetime.now()
return end-begin
if __name__ == '__main__':
print time_1()
import cProfile
cProfile.run('time_1()')
➜ Python python time_1.py
0:00:00.282828
2 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Traceback (most recent call last):
File "time_1.py", line 15, in <module>
cProfile.run('main()')
File "/usr/lib/python2.7/cProfile.py", line 29, in run
prof = prof.run(statement)
File "/usr/lib/python2.7/cProfile.py", line 135, in run
return self.runctx(cmd, dict, dict)
File "/usr/lib/python2.7/cProfile.py", line 140, in runctx
exec cmd in globals, locals
File "<string>", line 1, in <module>
NameError: name 'main' is not defined
➜ Python vi time_1.py
➜ Python python time_1.py
0:00:00.284642
5 function calls in 0.281 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.281 0.281 <string>:1(<module>)
1 0.281 0.281 0.281 0.281 time_1.py:3(time_1)
2 0.000 0.000 0.000 0.000 {built-in method now}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有