python Jupyter运行时间实例过程解析

yipeiwu_com6年前Python基础

这篇文章主要介绍了python Jupyter运行时间实例过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.Python time time()方法

import time
time_start=time.time()
time_end=time.time()
print('totally cost',time_end-time_start)
import time

print "time.time(): %f " % time.time()
print time.localtime( time.time() )
print time.asctime( time.localtime(time.time()) )

以上实例输出结果为:

time.time(): 1234892919.655932
(2009, 2, 17, 10, 48, 39, 1, 48, 0)
Tue Feb 17 10:48:39 2009

Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数)

参数:NA。

返回值:返回当前时间的时间戳(1970纪元后经过的浮点秒数)。

2.Jupyter Magic - Timing(%%time %time %timeit)

对于计时有两个十分有用的魔法命令:%%time 和 %timeit. 如果你有些代码运行地十分缓慢,而你想确定是否问题出在这里,这两个命令将会非常方便。

(1).%%time 将会给出cell的代码运行一次所花费的时间。

%%time
import time
for _ in range(1000):
  time.sleep(0.01)# sleep for 0.01 seconds
 
output:
CPU times: user 196 ms, sys: 21.4 ms, total: 217 ms
Wall time: 11.6 s

(2).%time 将会给出当前行的代码运行一次所花费的时间。

import numpy
%time numpy.random.normal(size=1000)
output:
Wall time: 1e+03 µs

(3)%timeit 使用Python的timeit模块,它将会执行一个语句100,000次(默认情况下),然后给出运行最快3次的平均值。

import numpy
%timeit numpy.random.normal(size=100)
 
output:
12.8 µs ± 1.25 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python socket实现多对多全双工通信的方法

服务器:#server.py #!/usr/bin/env python #-*-coding:utf-8-*- import sys import struct#将字符串打包为二进...

用Cython加速Python到“起飞”(推荐)

用Cython加速Python到“起飞”(推荐)

事先声明,标题没有把“Python”错打成“Cython”,因为要讲的就是名为“Cython”的东西。 Cython是让Python脚本支持C语言扩展的编译器,Cython能够将Pyt...

python中seaborn包常用图形使用详解

python中seaborn包常用图形使用详解

seaborn包是对matplotlib的增强版,需要安装matplotlib后才能使用。 所有图形都用plt.show()来显示出来,也可以使用下面的创建画布 fig,ax=plt...

详谈python http长连接客户端

背景: 线上机器,需要过滤access日志,发送给另外一个api 期初是单进程,效率太低,改为多进程发送后,查看日志中偶尔会出现异常错误(忘记截图了。。。) 总之就是端口不够用了报错 原...

python 生成目录树及显示文件大小的代码

python 生成目录树及显示文件大小的代码

比如 1--1      2--1           2    &nbs...