亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

ISO8601 轉換器的最快日期時間

ISO8601 轉換器的最快日期時間

慕桂英546537 2023-05-16 14:23:12
我正在尋找將 Python 日期時間對象呈現為 ISO8601 字符串的最快方法。有幾種不同的方法可以做到這一點,本機datetime模塊有strftime,isoformat并且只是平面舊的__str__。還有許多其他更奇特的方法,例如 Pendulum ( https://pendulum.eustace.io/ ),xml.utils.iso8601.tostring以及這里的其他一些方法https://wiki.python.org/moin/WorkingWithTime我希望有類似 ciso8601 的東西,它有一個很好的比較多種方法的基準: https: //github.com/closeio/ciso8601#benchmark
查看完整描述

1 回答

?
臨摹微笑

TA貢獻1982條經驗 獲得超2個贊

我對我能找到的所有不同方法進行了基準測試,結果如下:

  • datetime.strftime - 1000000 次循環的時間:9.262935816994286

  • 轉換為字符串- 1000000 次循環的時間:4.381643378001172

  • datetime isoformat - 1000000 次循環的時間:4.331578577999608

  • pendulum to_iso8601_string - 1000000 次循環的時間:18.471532950992696

  • rfc3339 - 1000000 次循環的時間:24.731586036010412

生成它的代碼是:

import timeit

from datetime import datetime

from pendulum import datetime as pendulum_datetime

from rfc3339 import rfc3339


dt = datetime(2011, 11, 4, 0, 5, 23, 283000)

pendulum_dt = pendulum_datetime(2011, 11, 4, 0, 5, 23, 283000)


repeats = 10**6


print('datetime strftime')

func1 = lambda: datetime.strftime(dt, "%Y-%m-%dT%H:%M:%S.%f%z")

print(func1())

print('Time for {0} loops: {1}'.format(

    repeats, timeit.timeit(func1, number=repeats))

    )


print('cast to string')

func2 = lambda: str(dt)

print(func2())

print('Time for {0} loops: {1}'.format(

    repeats, timeit.timeit(func2, number=repeats))

    )


print('datetime isoformat')

func3 = lambda: datetime.isoformat(dt)

print(func3())

print('Time for {0} loops: {1}'.format(

    repeats, timeit.timeit(func3, number=repeats))

    )


print('pendulum to_iso8601_string')

func4 = lambda: pendulum_dt.to_iso8601_string()

print(func4())

print('Time for {0} loops: {1}'.format(

    repeats, timeit.timeit(func4, number=repeats))

    )


print('rfc3339')

func5 = lambda: rfc3339(dt)

print(func5())

print('Time for {0} loops: {1}'.format(

    repeats, timeit.timeit(func5, number=repeats))

    )


查看完整回答
反對 回復 2023-05-16
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號