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创建只读属性对象的方法(ReadOnlyObject)

复制代码 代码如下:def ReadOnlyObject(**args):    dictBI = {}    args_n...

tensorflow 获取模型所有参数总和数量的方法

实例如下所示: from functools import reduce from operator import mul def get_num_params(): num_p...

Python二叉树的定义及常用遍历算法分析

本文实例讲述了Python二叉树的定义及常用遍历算法。分享给大家供大家参考,具体如下: 说起二叉树的遍历,大学里讲的是递归算法,大多数人首先想到也是递归算法。但作为一个有理想有追求的程序...

浅谈python之新式类

前言 本文中代码运行的python版本一律采取2.7.13 科普: 经典类:classic class 新式类:new-style class python2.2 之前并...

Python使用ConfigParser模块操作配置文件的方法

本文实例讲述了Python使用ConfigParser模块操作配置文件的方法。分享给大家供大家参考,具体如下: 一、简介 用于生成和修改常见配置文档,当前模块的名称在 python 3....