函数的补丁datetime和strptime性能的提高

monkeytime的Python项目详细描述


补丁datetime使用更快的strtime实现。

当格式字符串使用填充指令时,此改进了strtime。

这是受支持指令的列表(迄今为止最常见):

SUPPORTED_DIRECTIVES = ('%d', '%m', '%Y', '%H', '%M', '%S', '%f')

请致电:

datetime.supported_strptime(format_string)
# True if supported, False otherwise

平均来说,比stdlib版本快4倍,比stdlib版本快40倍 使用pypy时!

用法:

# insert this one line
from monkeytime import datetime
# Done migrating code to monkeytime!

from datetime import datetime

# Call it once with the specific format string
# It will build a new function to quickly parse the string, then run it on
# the string you passed it.
dt = datetime.strptime('2014-05-30T12:14:15.123456', '%Y-%m-%dT%H:%M:%S.%f')

# Now, whenever you use that format again, it will use the function it
# built already! All constructed functions are memoized.
# Since we usually only use one or two formats in our code to parse logs
# and such, the performance of constructing it is negligible.

您只需要一行“from monkeytime import datetime”,然后 strtime性能将显著提高。

确保它位于“from datetime import datetime”导入行之上!

性能示例:

$ python timeit_test.py
Testing builtin strptime
testing ('2015-01-02 03:04:05.001234', '%Y-%m-%d %H:%M:%S.%f')
6.80288290977 seconds
testing ('05-06 12:15:18', '%m-%d %H:%M:%S')
5.81049013138 seconds
testing ('2010', '%Y')
4.29107117653 seconds
testing ('1905/08/05', '%Y/%m/%d')
4.92634987831 seconds
testing ('14:05:03.123456', '%H:%M:%S.%f')
5.6812889576 seconds
Testing monkeytime strptime
testing ('2015-01-02 03:04:05.001234', '%Y-%m-%d %H:%M:%S.%f')
1.83126211166 seconds
testing ('05-06 12:15:18', '%m-%d %H:%M:%S')
1.5586848259 seconds
testing ('2010', '%Y')
0.877351999283 seconds
testing ('1905/08/05', '%Y/%m/%d')
1.24154901505 seconds
testing ('14:05:03.123456', '%H:%M:%S.%f')
1.3871409893 seconds
3.714860 times as fast
3.727816 times as fast
4.890935 times as fast
3.967906 times as fast
4.095682 times as fast
Average: 4.079440 times as fast

$ pypy timeit_test.py
Testing builtin strptime
testing ('2015-01-02 03:04:05.001234', '%Y-%m-%d %H:%M:%S.%f')
2.19319605827 seconds
testing ('05-06 12:15:18', '%m-%d %H:%M:%S')
1.60669994354 seconds
testing ('2010', '%Y')
0.858637809753 seconds
testing ('1905/08/05', '%Y/%m/%d')
1.19449591637 seconds
testing ('14:05:03.123456', '%H:%M:%S.%f')
1.38721394539 seconds
Testing monkeytime strptime
testing ('2015-01-02 03:04:05.001234', '%Y-%m-%d %H:%M:%S.%f')
0.0362730026245 seconds
testing ('05-06 12:15:18', '%m-%d %H:%M:%S')
0.0450730323792 seconds
testing ('2010', '%Y')
0.0332229137421 seconds
testing ('1905/08/05', '%Y/%m/%d')
0.0321681499481 seconds
testing ('14:05:03.123456', '%H:%M:%S.%f')
0.0355319976807 seconds
60.463593 times as fast
35.646591 times as fast
25.844747 times as fast
37.132876 times as fast
39.041260 times as fast
Average: 39.625813 times as fast

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何在javaswing中使用进度条,同时函数在繁重的进程中工作   SWIG(Java):如何调用带有void*out参数的函数?   java帮助解决算法问题   java,但我不想用try/catch块来包围语句!   可以从java类调用安卓类吗?   java流拆分器实现细节   java组织。莫基托。例外情况。滥用。无效使用MatcherException   java如何显示接收到的字符串?   java为什么这段代码不像JSR133建议的那样进入无限循环?   java BufferedReader readLine()从socket获取数据时冻结   java如何使用html表单发送的XML?   java RxJava与观察者代码的并行执行   我想使用CoreJava基于文本输入动态创建一个jpg图像   java如何在Mockito中模拟注入的bean?   java我的程序突然停止工作,我没有改变我记得的任何东西,现在它给出了第二个错误:javafx。fxml。加载异常   java树集排序错误   java RSA加密解密AES密钥并存储在文件中   Java将变量字符串[]与字符串混合在一个字符串数组中   如何在鼠标移动时重新绘制Java SWT应用程序?