Python 中 time模块 time.time() 是不是系统调用
2017-07-09 16:30
537 查看
事情是这样的,最近公司自己定义了一个api规范,其中有一部分是请求时候用时间戳做 md5摘要。 然后我看到领导在文档中说 python 的
线上的环境一般是 CentOS6.7 , CentOS 7, Python 的版本 2.6(惭愧),2.7。
下面的测试环境是 Centos6.7, Python2.6,我先来自己测测,然后在google吧。
两个问题
* 我们的环境中 time.time() 是否有系统调用?
* 这个函数的 cost 大到了无法承受的地步吗?
原文地址 http://blog.csdn.net/orangleliu/article/details/74892564
我们知道 linux 中time 命令可以显示出来 系统态和内核态使用的时间,先用 time 命令测下。
结果叫人失望, 看起来 内核态没有多少时间,貌似不是个系统调用。
看看结果
可以看到主要的开销,并不在 timemodule 啊。
开始找资料,首先是源码 python2.7 timemodule
既然这里提到了
根据 Two frequently used system calls are ~77% slower on AWS EC2 ,这篇文章
编译完成用 strace 测试
并没发现系统调用。
最后来看看 陶辉老师的这篇文章吧 浅谈时间函数gettimeofday的成本
x86_64体系,gettimeofday是C库提供的函数(不是系统调用),它封装了内核里的sys_gettimeofday系统调用,就是说,归根到底是系统调用。
对于x86_64系统来说,这是个虚拟系统调用vsyscall!所以,这里它不用发送中断!速度很快,成本低,调用一次的成本大概不到一微秒!
好了,结果比较明显了,今天学到了点新知识,挺高兴!
time.time()是系统系统调用,调用的消耗太大,md5摘要中不用时间戳,而是用其他的字段。 于是我就不太相信这个说法。
线上的环境一般是 CentOS6.7 , CentOS 7, Python 的版本 2.6(惭愧),2.7。
下面的测试环境是 Centos6.7, Python2.6,我先来自己测测,然后在google吧。
两个问题
* 我们的环境中 time.time() 是否有系统调用?
* 这个函数的 cost 大到了无法承受的地步吗?
原文地址 http://blog.csdn.net/orangleliu/article/details/74892564
测试一
先写个简单的测试脚本吧, timetest.py# coding:utf-8 import time for i in xrange(10000000): time.time()
我们知道 linux 中time 命令可以显示出来 系统态和内核态使用的时间,先用 time 命令测下。
> time python timetest.py real 0m1.793s user 0m1.790s sys 0m0.002s
结果叫人失望, 看起来 内核态没有多少时间,貌似不是个系统调用。
测试二
linux上有个使用的 工具叫做 perf,可以统计cpu使用时间。> perf record -F 99 python timetest.py [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.017 MB perf.data (~730 samples) ] > perf report
看看结果
28.84% python libpython2.6.so.1.0 [.] PyEval_EvalFrameEx 9.43% python [kernel.kallsyms] [.] vread_tsc 8.32% python libpython2.6.so.1.0 [.] PyDict_GetItem 5.72% python [kernel.kallsyms] [k] __wake_up_bit 4.44% python [vdso] [.] __vdso_gettimeofday 3.88% python libpython2.6.so.1.0 [.] _PyType_Lookup 3.33% python libpython2.6.so.1.0 [.] PyObject_GenericGetAttr 2.22% python libpython2.6.so.1.0 [.] 0x00000000000795a9 1.66% python libpython2.6.so.1.0 [.] 0x00000000000795b0 1.66% python [vdso] [.] 0x000000000000082a 1.66% python libpython2.6.so.1.0 [.] PyDict_SetItem 1.66% python libpython2.6.so.1.0 [.] 0x00000000000796f0 1.11% python timemodule.so [.] 0x0000000000001e2a 1.11% python libpython2.6.so.1.0 [.] 0x00000000000796f8 1.11% python libpython2.6.so.1.0 [.] 0x00000000000795fd 1.11% python [vdso] [.] 0x0000000000000873 1.11% python libpython2.6.so.1.0 [.] 0x0000000000079605 1.11% python libpython2.6.so.1.0 [.] 0x000000000007823c 1.11% python [vdso] [.] 0x00000000000007e4 1.11% python timemodule.so [.] 0x0000000000001f13 1.11% python libpython2.6.so.1.0 [.] PyObject_GetAttr@plt 1.11% python libpython2.6.so.1.0 [.] _PyType_Lookup@plt 1.11% python libpython2.6.so.1.0 [.] PyInt_FromLong 0.55% python libpython2.6.so.1.0 [.] PyFloat_FromDouble 0.55% python [vdso] [.] 0x000000000000081f .... 0.55% python timemodule.so [.] 0x0000000000001e32 .... 0.55% python timemodule.so [.] gettimeofday@plt
可以看到主要的开销,并不在 timemodule 啊。
思考
从上面的测试来看 ,这个函数的调用开销没想象中的那么大,看起来也不像是个系统调用。开始找资料,首先是源码 python2.7 timemodule
static double floattime(void) { /* There are three ways to get the time: (1) gettimeofday() -- resolution in microseconds (2) ftime() -- resolution in milliseconds (3) time() -- resolution in seconds In all cases the return value is a float in seconds. Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may fail, so we fall back on ftime() or time(). Note: clock resolution does not imply clock accuracy! */ #ifdef HAVE_GETTIMEOFDAY { struct timeval t; #ifdef GETTIMEOFDAY_NO_TZ if (gettimeofday(&t) == 0) return (double)t.tv_sec + t.tv_usec*0.000001; #else /* !GETTIMEOFDAY_NO_TZ */ if (gettimeofday(&t, (struct timezone *)NULL) == 0) return (double)t.tv_sec + t.tv_usec*0.000001; #endif /* !GETTIMEOFDAY_NO_TZ */ } #endif /* !HAVE_GETTIMEOFDAY */ { #if defined(HAVE_FTIME) struct timeb t; ftime(&t); return (double)t.time + (double)t.millitm * (double)0.001; #else /* !HAVE_FTIME */ time_t secs; time(&secs); return (double)secs; #endif /* !HAVE_FTIME */ } }
既然这里提到了
gettimeofday,那么来看看这个调用吧。man 手册里面并未提到是系统调用
根据 Two frequently used system calls are ~77% slower on AWS EC2 ,这篇文章
#include <stdio.h> #include <stdlib.h> #include <sys/time.h> int main(int argc, char *argv[]) { struct timeval tv; int i = 0; for (; i<100; i++) { gettimeofday(&tv,NULL); } return 0; }
编译完成用 strace 测试
> gcc -o test.exe test.c > strace -ce gettimeofday ./test.exe
并没发现系统调用。
最后来看看 陶辉老师的这篇文章吧 浅谈时间函数gettimeofday的成本
x86_64体系,gettimeofday是C库提供的函数(不是系统调用),它封装了内核里的sys_gettimeofday系统调用,就是说,归根到底是系统调用。
对于x86_64系统来说,这是个虚拟系统调用vsyscall!所以,这里它不用发送中断!速度很快,成本低,调用一次的成本大概不到一微秒!
好了,结果比较明显了,今天学到了点新知识,挺高兴!
相关文章推荐
- python使用ctypes模块调用windowsapi获取系统版本示例
- python模块:调用系统命令模块subprocess等
- Python常用模块种类的系统介绍及与操作系统相关的调用和操作
- NAO机器人系统下模块生成和python模块调用
- Python常用模块种类的系统介绍及与操作系统相关的调用和操作
- Python模块之subprocess--使用Popen来调用系统命令
- Python常用模块种类的系统介绍及与操作系统相关的调用和操作
- Python常用模块种类的系统介绍及与操作系统相关的调用和操作
- NAO机器人系统下模块生成和python模块调用
- Python常用模块种类的系统介绍及与操作系统相关的调用和操作
- 一段用Python语言的Ctypes模块调用系统api函数写的rsa加密代码
- python使用ctypes模块调用windowsapi获取系统版本示例
- python模块:调用系统命令模块subprocess等
- 使用c写python的模块和python调用系统dll
- python的计时函数——timeit模块
- gettimeofday/settimeofday系统调用的说明(ZZ)
- linux C 调用系统接口安装模块 范例
- 使用Python的timeit模块
- 【转载】在 Linux 下用户空间与内核空间数据交换的方式,第 1 部分: 内核启动参数、模块参数与sysfs、sysctl、系统调用和netlink
- python time模块详解(转)