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

yipeiwu_com5年前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设计】。

相关文章

Linux上安装Python的PIL和Pillow库处理图片的实例教程

安装 正常情况,只需 pip install PIL==1.1.7 或者 pip install Pillow==2.9.0 即可。但需留意安装后的输出 安装完成后,需留意输...

对python 通过ssh访问数据库的实例详解

通常,为了安全性,数据库只允许通过ssh来访问。例如:mysql数据库放在服务器A上,只允许数据库B来访问,这时,我们需要用机器C去访问数据库,就需要用C通过ssh连接B,再访问A。 通...

Python之自动获取公网IP的实例讲解

Python之自动获取公网IP的实例讲解

0.预备知识 0.1 SQL基础 ubuntu、Debian系列安装: root@raspberrypi:~/python-script# apt-get install mysql...

打开电脑上的QQ的python代码

复制代码 代码如下:# _*_ coding:utf-8 _*_# name start_qq.pyimport osos.startfile("C:\Program Files\Ten...

Python pass详细介绍及实例代码

Python pass的用法: 空语句 do nothing 保证格式完整 保证语义完整 以if语句为例,在c或c++/Java中: if(true) ; //do...