timeit

直接举例

  • 必要的导入
import timeit

1. 测量生成列表的时间

  • 像是 C 或者 Js 中把函数作为参数传入
>>> func = '''
... arr = []
... for i in range(1000):
... arr.append(i)
... '''
>>> t1 = timeit.timeit(stmt=func, number=10000)
>>> t2 = timeit.timeit(stmt="[i for i in range(1000)]", number=10000)
>>> t1
1.0912232999999105
>>> t2
0.5270981999999549
  • 可以看出,列表生成式比 "append" 快

2. 测量函数运行时间(一)

  • 公共部分
>>> def func(num=3):
... for i in range(num):
... print(f"Repeat for {i}.")
...
>>>
  • 版本一
>>> t = timeit.timeit(stmt=func, number=5)
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
Repeat for 0.
Repeat for 1.
Repeat for 2.
>>> t
0.0009049000000231899
  • 版本二
>>> t = timeit.timeit(stmt=func, setup="func"+"num=5", number=5)
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
>>> t
0.008177499999874271
  • t = timeit.timeit(func, setup="funcnum=5", number=5) 也行,但不直观

3. 测量函数运行时间(二)

>>> s = '''
... def func(num):
... for i in range(num):
... print(f"Repeat for {i}")
... '''
>>> t = timeit.timeit(stmt="func(num)", setup=s+"num=3", number=5)
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
Repeat for 0
Repeat for 1
Repeat for 2
>>> t
0.0020025999997415056

[Python3] 029 常用模块 timeit的更多相关文章

  1. 09 . Python3之常用模块

    模块的定义与分类 模块是什么? 一个函数封装一个功能,你使用的软件可能就是由n多个函数组成的(先备考虑面向对象).比如抖音这个软件,不可能将所有程序都写入一个文件,所以咱们应该将文件划分,这样其组织结 ...

  2. 【Python3之常用模块】

    一.time 1.三种表达方式 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.命令如下 ...

  3. [Python3] 032 常用模块 random

    目录 random 1. random.random() 2. random.choice() 3. random.shuffle() 4. random.randint() 5. random.ra ...

  4. [Python3] 031 常用模块 shutil & zipfile

    目录 shutil 1. shutil.copy() 2. shutil.copy2() 3. shutil.copyfile() 4. shutil.move() 5. 归档 5.1 shutil. ...

  5. [Python3] 030 常用模块 os

    目录 os 1. os.getcwd() 2. os.chdir() 3. os.listdir() 4. os.makedir() 5. os.system() 6. os.getenv() 7. ...

  6. [Python3] 028 常用模块 datetime

    目录 datetime 1. datetime.date 2. datetime.time 3. datetime.datetime 4. datetime.timedelta 补充 datetime ...

  7. [Python3] 027 常用模块 time

    目录 time 1. 时间戳 2. UTC 时间 3. 夏令时 4. 时间元组 5. 举例 5.1 例子1 例子2 例子3 例子4 例子5 例子6 例子7 time 1. 时间戳 一个时间表示,根据不 ...

  8. [Python3] 026 常用模块 calendar

    目录 calendar 1. calendar.calendar(year, w, l, c, m) 2. calendar.prcal(year, w, l, c, m) 3. calendar.m ...

  9. python3 常用模块详解

    这里是python3的一些常用模块的用法详解,大家可以在这里找到它们. Python3 循环语句 python中模块sys与os的一些常用方法 Python3字符串 详解 Python3之时间模块详述 ...

随机推荐

  1. 2019春Python程序设计练习5(0416--0422)

    6-1 6-1.使用函数求特殊a串数列和 (30 分)   给定两个均不超过9的正整数a和n,要求编写函数fn(a,n) 求a+aa+aaa++⋯+aa⋯aa(n个a)之和,fn须返回的是数列和 函数 ...

  2. 用SWPM导入SAP时因用户Culture及系统Locale引发的问题

    [问题]SWPM安装SAP时因用户Culture及系统Locale引发的问题 [现象] ①IE浏览器显示空白(SWPM界面不显示) ②SAP安装时出现“Error occurs when execut ...

  3. java正则表达式验证邮箱、手机号码

    /** * 验证邮箱地址是否正确 * @param email * @return */ public static boolean checkEmail(String email){ boolean ...

  4. hdu 1208 Ignatius and the Princess III 划分数,dp

    题目 题意:给你一个数字n,求将其划分成若干个数字相加总共有多少种划分数: <span style="font-size:24px;">#include <ios ...

  5. 【UOJ#129】 【NOI2015】寿司晚宴

    题目描述 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同的寿司,编号 ...

  6. Jmeter(一) 安装

    一.检查JDK版本 执行cmd > java -version 查看本机JDK版本,JDK版本不能低于1.6 二.软件下载 登录Jmeter官网:https://jmeter.apache.or ...

  7. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  8. 12.Python数值类型(整形、浮点型和复数)及其用法

    实际开发中,我们经常需要使用数字记录游戏中用户的得分.游戏中角色的生命值.伤害值等信息,Python 语言提供了数值类型用于保存这些数值. 需要注意的是,Python 中这些数值类型都是不可改变的,也 ...

  9. 删除全局安装的npm包

    方法一 : 使用命令 npm uninstall -g 包名 方法二: 直接找到对应包删除 C:\Users\自己用户的文件夹\AppData\Roaming\npm 将对应的包删除即可

  10. 记一次AD域控客户端不能正常加域的故障处理

    1.1  症状现象 DNS 服务器无法在应用程序目录分区 < 分区名称 > 从 Active Directory 中打开区域 < 区域 >.这台 DNS 服务器配置获取并使用此 ...