pytz 时区转换性能
pytz timezone conversion performance
我有超过 100 万个来自数据库的日期时间对象,我想将它们中的每一个都转换为可识别时区的日期时间对象。这是我的辅助函数 conv_tz:
# dt is python datetime object, src_tz and dest_tz and pytz.timezone objects
def conv_tz(dt, src_tz, dest_tz):
if not dt: return None
sdt = src_tz.localize(dt)
return sdt.astimezone(dest_tz)
这是探查器的结果:
ncalls tottime percall cumtime percall filename:lineno(function)
1101475 1.166 0.000 44.440 0.000 ../release/python/lib/dtutil.py:128(conv_tz)
1101475 9.092 0.000 35.656 0.000 /app/python/lib/python3.4/site-packages/pytz/tzinfo.py:244(localize)
问题 1:有没有办法让它 运行 更快?假设数据库中的每个日期时间对象都在 pytz.timezone('America/New_York') 中,并且目标时区因每个日期时间对象(或数据库中的每一行)而异
事实上,在我得到时区感知的 datetime 对象之后,我真正想要实现的是将这些 datetime 对象转换为 matlab 时间(不支持时区。)所以这里是 to_mat我使用的功能:
def to_mat(dt):
if not dt: return None
val = dt.toordinal() + 366
t = dt.time()
return val + (((t.hour * 60) + t.minute) * 60 + t.second) / float(_seconds_day) + t.microsecond / 1.0e6 / _seconds_day
我正在为超过 100 万个日期时间对象一起调用这 2 个函数:
matdt = dtutil.to_mat(dtutil.conv_tz(dt, pytz.timezone('America/New_York'), dst_tz))
问题2:也许有更好的方法将这些转换一起进行?这里是to_mat的profiler,貌似比conv_tz:
耗时少
3304425 5.067 0.000 5.662 0.000 ../release/python/lib/dtutil.py:8(to_mat)
环境:CentOS6 x64 + Python3.4.3 x64
感谢 J.F。塞巴斯蒂安评论!这是我决定使用的,假设这些数据时间对象的默认时区与 OS 时区一致:
def conv_tz2(dt, dest_tz):
if not dt: return None
return datetime.fromtimestamp(dt.timestamp(), dest_tz)
它只运行原始 conv_tz 的一小部分。这是基于五十万转换的测试:
ncalls tottime percall cumtime percall filename:lineno(function)
567669 0.664 0.000 23.354 0.000 ../test/test_tz.py:17(conv_tz)
567669 4.831 0.000 18.732 0.000 /app/python/lib/python3.4/site-packages/pytz/tzinfo.py:244(localize)
567669 0.472 0.000 5.786 0.000 ../test/test_tz.py:22(conv_tz2)
我有超过 100 万个来自数据库的日期时间对象,我想将它们中的每一个都转换为可识别时区的日期时间对象。这是我的辅助函数 conv_tz:
# dt is python datetime object, src_tz and dest_tz and pytz.timezone objects
def conv_tz(dt, src_tz, dest_tz):
if not dt: return None
sdt = src_tz.localize(dt)
return sdt.astimezone(dest_tz)
这是探查器的结果:
ncalls tottime percall cumtime percall filename:lineno(function)
1101475 1.166 0.000 44.440 0.000 ../release/python/lib/dtutil.py:128(conv_tz)
1101475 9.092 0.000 35.656 0.000 /app/python/lib/python3.4/site-packages/pytz/tzinfo.py:244(localize)
问题 1:有没有办法让它 运行 更快?假设数据库中的每个日期时间对象都在 pytz.timezone('America/New_York') 中,并且目标时区因每个日期时间对象(或数据库中的每一行)而异
事实上,在我得到时区感知的 datetime 对象之后,我真正想要实现的是将这些 datetime 对象转换为 matlab 时间(不支持时区。)所以这里是 to_mat我使用的功能:
def to_mat(dt):
if not dt: return None
val = dt.toordinal() + 366
t = dt.time()
return val + (((t.hour * 60) + t.minute) * 60 + t.second) / float(_seconds_day) + t.microsecond / 1.0e6 / _seconds_day
我正在为超过 100 万个日期时间对象一起调用这 2 个函数:
matdt = dtutil.to_mat(dtutil.conv_tz(dt, pytz.timezone('America/New_York'), dst_tz))
问题2:也许有更好的方法将这些转换一起进行?这里是to_mat的profiler,貌似比conv_tz:
耗时少3304425 5.067 0.000 5.662 0.000 ../release/python/lib/dtutil.py:8(to_mat)
环境:CentOS6 x64 + Python3.4.3 x64
感谢 J.F。塞巴斯蒂安评论!这是我决定使用的,假设这些数据时间对象的默认时区与 OS 时区一致:
def conv_tz2(dt, dest_tz):
if not dt: return None
return datetime.fromtimestamp(dt.timestamp(), dest_tz)
它只运行原始 conv_tz 的一小部分。这是基于五十万转换的测试:
ncalls tottime percall cumtime percall filename:lineno(function)
567669 0.664 0.000 23.354 0.000 ../test/test_tz.py:17(conv_tz)
567669 4.831 0.000 18.732 0.000 /app/python/lib/python3.4/site-packages/pytz/tzinfo.py:244(localize)
567669 0.472 0.000 5.786 0.000 ../test/test_tz.py:22(conv_tz2)