1 import time
  2
  3 # 2种表示时间的方法:
  4 # (1)时间戳,从1970.1.1开始
  5 # (2)一个9个整数的元组
  6 #    这个元组内容:
  7 #       year (including century, e.g. 1998)
  8 #       month (1-12)
  9 #       day (1-31)
 10 #       hours (0-23)
 11 #       minutes (0-59)
 12 #       seconds (0-59)
 13 #       weekday (0-6, Monday is 0)
 14 #       Julian day (day in the year, 1-366)
 15 #       DST (Daylight Savings Time) flag (-1, 0 or 1)
 16 #          1 if summer time is in effect, 0 if not, and -1 if unknown
 17
 18
 19 # FUNCTIONS
 20 """
 21     time(...)
 22         time() -> floating point number
 23
 24         Return the current time in seconds since the Epoch.
 25         Fractions of a second may be present if the system clock provides them.
 26         本地的当前时间的时间戳
 27 """
 28 print(time.time())
 29
 30 """
 31     localtime(...)
 32         localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,
 33                                   tm_sec,tm_wday,tm_yday,tm_isdst)
 34
 35         Convert seconds since the Epoch to a time tuple expressing local time.
 36         When 'seconds' is not passed in, convert the current time instead.
 37         没有参数时本地的当前时间的元组形式
 38 """
 39 print(time.localtime())
 40
 41 """
 42     gmtime(...)
 43         gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,
 44                                tm_sec, tm_wday, tm_yday, tm_isdst)
 45
 46         Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.
 47         GMT).  When 'seconds' is not passed in, convert the current time instead.
 48
 49         If the platform supports the tm_gmtoff and tm_zone, they are available as
 50         attributes only.
 51 """
 52 print(time.gmtime(time.time()))
 53
 54
 55 """
 56     mktime(...)
 57         mktime(tuple) -> floating point number
 58
 59         Convert a time tuple in local time to seconds since the Epoch.
 60         Note that mktime(gmtime(0)) will not generally return zero for most
 61         time zones; instead the returned value will either be equal to that
 62         of the timezone or altzone attributes on the time module.
 63 """
 64 print(time.mktime(time.localtime()))
 65
 66
 67 """
 68     asctime(...)
 69         asctime([tuple]) -> string
 70
 71         Convert a time tuple to a string, e.g. 'Sat Jun 06 16:26:11 1998'.
 72         When the time tuple is not present, current time as returned by localtime()
 73         is used.
 74         tuple转字符串,默认是当前时间
 75 """
 76 print(time.asctime())   # Thu Jan 18 16:36:07 2018
 77
 78 """
 79     ctime(...)
 80         ctime(seconds) -> string
 81
 82         Convert a time in seconds since the Epoch to a string in local time.
 83         This is equivalent to asctime(localtime(seconds)). When the time tuple is
 84         not present, current time as returned by localtime() is used.
 85         时间戳转字符串,默认是当前时间
 86 """
 87 print(time.ctime())
 88
 89 """
 90     strftime(...)
 91         strftime(format[, tuple]) -> string
 92
 93         Convert a time tuple to a string according to a format specification.
 94         See the library reference manual for formatting codes. When the time tuple
 95         is not present, current time as returned by localtime() is used.
 96
 97         Commonly used format codes:
 98
 99         %Y  Year with century as a decimal number.
100         %m  Month as a decimal number [01,12].
101         %d  Day of the month as a decimal number [01,31].
102         %H  Hour (24-hour clock) as a decimal number [00,23].
103         %M  Minute as a decimal number [00,59].
104         %S  Second as a decimal number [00,61].
105         %z  Time zone offset from UTC.      +0800
106         %a  Locale's abbreviated weekday name.      Thu
107         %A  Locale's full weekday name.      Thursday
108         %b  Locale's abbreviated month name.     Jan
109         %B  Locale's full month name.     January
110         %c  Locale's appropriate date and time representation. Thu Jan 18 16:53:35 2018
111         %I  Hour (12-hour clock) as a decimal number [01,12].  04
112         %p  Locale's equivalent of either AM or PM.            PM
113
114         Other codes may be available on your platform.  See documentation for
115         the C library strftime function.
116         格式化元组到字符串
117 """
118 print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()))
119
120 """
121     strptime(...)
122         strptime(string, format) -> struct_time
123
124         Parse a string to a time tuple according to a format specification.
125         See the library reference manual for formatting codes (same as
126         strftime()).
127
128         Commonly used format codes:
129
130         %Y  Year with century as a decimal number.
131         %m  Month as a decimal number [01,12].
132         %d  Day of the month as a decimal number [01,31].
133         %H  Hour (24-hour clock) as a decimal number [00,23].
134         %M  Minute as a decimal number [00,59].
135         %S  Second as a decimal number [00,61].
136         %z  Time zone offset from UTC.
137         %a  Locale's abbreviated weekday name.
138         %A  Locale's full weekday name.
139         %b  Locale's abbreviated month name.
140         %B  Locale's full month name.
141         %c  Locale's appropriate date and time representation.
142         %I  Hour (12-hour clock) as a decimal number [01,12].
143         %p  Locale's equivalent of either AM or PM.
144
145         Other codes may be available on your platform.  See documentation for
146         the C library strftime function.
147         字符串到元组
148 """
149 #
150 print(time.strptime('2018-01-18 16:55:01', '%Y-%m-%d %H:%M:%S'))
151 print(time.strptime('Thu Jan 18 16:55:56 2018', '%c'))
152
153 """
154     sleep(...)
155         sleep(seconds)
156
157         Delay execution for a given number of seconds.  The argument may be
158         a floating point number for subsecond precision.
159 """
160 """
161     clock(...)
162         clock() -> floating point number
163
164         Return the CPU time or real time since the start of the process or since
165         the first call to clock().  This has as much precision as the system
166         records.
167 """
168 def func():
169     sum = 0
170     for i in range(1000):
171         sum = sum + 1
172         time.sleep(0.01)
173
174 t0 = time.clock()
175 p0 = time.time()
176 print(t0, p0)
177 func()
178 t1 = time.clock()
179 p1 = time.time()
180 print(t1, p1)
181 print(t1-t0, p1-p0)   # 0.08869099999999999 10.986821174621582
182 # linux端,clock只统计cpu运行的时间,执行语句,sleep不统计。windows不一样
183
184 """
185     get_clock_info(...)
186         get_clock_info(name: str) -> dict
187
188         Get information of the specified clock.
189 """
190 print(time.get_clock_info('clock'))
191 print(time.get_clock_info('monotonic'))
192 print(time.get_clock_info('perf_counter'))
193 print(time.get_clock_info('process_time'))
194 print(time.get_clock_info('time'))
195
196
197 """
198     monotonic(...)
199         monotonic() -> float
200
201         Monotonic clock, cannot go backward.
202         系统启动,开启的一个时钟,也就是系统开启时间
203 """
204
205
206 """
207     perf_counter(...)
208         perf_counter() -> float
209
210         Performance counter for benchmarking.
211         系统运行时间
212 """
213
214
215 """
216     process_time(...)
217         process_time() -> float
218
219         Process time for profiling: sum of the kernel and user-space CPU time.
220 """
221 def func():
222     sum = 0
223     for i in range(1000):
224         sum = sum + 1
225         time.sleep(0.01)
226
227 t0 = time.process_time()
228 p0 = time.time()
229 print(t0, p0)
230 func()
231 t1 = time.process_time()
232 p1 = time.time()
233 print(t1, p1)
234 print(t1-t0, p1-p0)   # 0.08869099999999999 10.986821174621582
235 # 统计内核和用户空间占用cpu的时间

转载于:https://www.cnblogs.com/gundan/p/8311183.html

python time模块相关推荐

  1. Python Re 模块超全解读!详细

    内行必看!Python Re 模块超全解读! 2019.08.08 18:59:45字数 953阅读 121 re模块下的函数 compile(pattern):创建模式对象 > import ...

  2. python argparse模块_Python argparse模块应用实例解析

    这篇文章主要介绍了Python argparse模块应用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 简介 argparse是python ...

  3. 关于使用python logging模块的几点总结

    关于使用python logging模块的几点总结 使用python的标准日志模块logging可以非常方便地记录日志.Python日志系统非常丰富.添加结构化或非结构化日志输出到python代码,写 ...

  4. python高级-模块(14)

    一.python中的模块 有过C语言编程经验的朋友都知道在C语言中如果要引用sqrt函数,必须用语句#include <math.h>引入math.h这个头文件,否则是无法正常进行调用的. ...

  5. 转载: Python os 模块的功能以及子函数介绍

    原文链接: python之os模块 - 程序生(Codey) - 博客园 https://www.cnblogs.com/cxscode/p/8085326.html 一.Python OS模块介绍 ...

  6. 简单介绍python process模块

    在python中大部分情况需要使用多进程,python提供了multiprocessing模块.multiprocessing模块的功能众多:支持子进程.通信和共享数据.执行不同形式的同步,提供了Pr ...

  7. python io模块_python中的StringIO模块

    原博文 2015-10-23 15:21 − # python中的StringIO模块 标签:python StringIO --- > 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的, ...

  8. python正则表达式需要模块_使用Python正则表达式模块,让操作更加简单

    处理文本数据的一个主要任务就是创建许多以文本为基础的特性. 人们可能想要在文本中找出特定格式的内容,比如找出存在于文本中的电子邮件,或者大型文本中的电话号码. 虽然想要实现上述功能听起来很繁琐,但是如 ...

  9. python导入模块有同名_Python:导入与函数同名的模块

    背景:第一次在SE上提问.我在 Python方面还很陌生,而且在编程方面也不是很有经验.我已经四处寻找,但我没有找到这个问题的答案,我非常感谢你的帮助. 我的问题是:如何导入与函数同名的模块? 具体来 ...

  10. python第三方模块—psutil模块

    系统基础信息采集模块作为监控模块的重要组成部分,能够帮助运维人员了解当前系统的健康程度,同时也是衡量业务的服务质量的依据,比如系统资源吃紧,会直接影响业务的服务质量及用户体验,另外获取设备的流量信息, ...

最新文章

  1. pta简单实现x的n次方_PTA-2017实验2.4 函数
  2. 不用 IDE 手工创建、开发、编译、安装 Android 应用程
  3. oracle导出数据dummy,oracle导出表结构1
  4. C#中使用NPIO实现导入导出Excel简单操作
  5. python pandas检验一列中是否只有一个值
  6. 条件锁pthread_cond_t
  7. 安卓平台中的动态加载技术分析
  8. 换了路由器电脑都连不上网了_如果你连汽滤多久换一次,都不知道,就不要说自己是老司机了...
  9. python好用的模块_Python中好用的模块们
  10. 【luogu/字符串】多项式输出(所有情况一起处理)
  11. 太原市消防工程师培训_关于消防工程师的满满干货
  12. 长歌行 宋 郭茂倩收编的《乐府歌词》汉代民间诗歌
  13. A survey on challenges and progresses in blockchain technologies区块链综述
  14. C宏#define的一些用法
  15. linux 内存越界判断_linux 内存越界判断
  16. (d2l-ai/d2l-zh)《动手学深度学习》pytorch 笔记(4)线性神经网络(暂停)
  17. idea 跳转到方法调用处
  18. 最合适触屏方法 指划修图Snapseed
  19. mysql索引怎么设计,MySQL如何设计索引
  20. 移植OpenHarmony 3.0到ARM单片机

热门文章

  1. Linux下的Vsftpd配置篇
  2. 开发直播APP选择云服务器的优点
  3. hive臨時udf與永久udf詳細操作流程
  4. windows新添开机启动项
  5. 一句话讲清楚GIL锁
  6. numpy.matrixlib.defmatrix.matrix写入csv文件
  7. 决策树-熵计算-ID3算法(转)
  8. sublime自定义主题-修改行号的颜色
  9. python中的协程:greenlet和gevent
  10. 2020 华为杯 数模 B题 数据挖掘