亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python中判斷子串存在的性能比較及分析總結(jié)

 更新時間:2019年06月23日 08:40:03   作者:棲遲于一丘  
這篇文章主要給大家總結(jié)介紹了Python中判斷子串存在的性能比較及分析的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧

起步

對于子串搜索,Python提供了多種實現(xiàn)方式:in, find, index, __contains__,對其進行性能比較:

import timeit

def in_(s, other):
  return other in s

def contains(s, other):
  return s.__contains__(other)

def find(s, other):
  return s.find(other) != -1

def index(s, other):
  try:
    s.index(other)
  except ValueError:
    return False
  return True

perf_dict = {
  'in:True': min(timeit.repeat(lambda: in_('superstring', 'str'))),
  'in:False': min(timeit.repeat(lambda: in_('superstring', 'not'))),
  '__contains__:True': min(timeit.repeat(lambda: contains('superstring', 'str'))),
  '__contains__:False': min(timeit.repeat(lambda: contains('superstring', 'not'))),
  'find:True': min(timeit.repeat(lambda: find('superstring', 'str'))),
  'find:False': min(timeit.repeat(lambda: find('superstring', 'not'))),
  'index:True': min(timeit.repeat(lambda: index('superstring', 'str'))),
  'index:False': min(timeit.repeat(lambda: index('superstring', 'not'))),
}

print(perf_dict)

得到結(jié)果:

{
    'in:True': 0.2763608000000001,
    'in:False': 0.2794432,
    '__contains__:True': 0.40546490000000013,
    '__contains__:False': 0.4122471000000001,
    'find:True': 0.497128,
    'find:False': 0.4951530000000002,
    'index:True': 0.5243821999999998,
    'index:False': 0.8693923999999988
}

從結(jié)果上 in 的搜索方式性能上最好。

知其然也要之其所以然,下面就對于這個結(jié)果進行比較與分析。

in 與 __contains__ 比較

了解 Python 中協(xié)議的應該知道,in 操作其實也是調(diào)用 __contains__ ,但為什么 in 比 __contains__ 明顯快了很多,明明它們最終調(diào)用的C語言函數(shù)是一樣的。

在 CPython 中,in 屬于操作符,它直接指向了 sq_contains 中的C級函數(shù)指針,而在 str 中的 sq_contains 直接指向了最終調(diào)用的C層函數(shù)。而 __contains__ 的調(diào)用方式,則需要先在 str 屬性中進行 LOAD_ATTR 查找,然后再為 CALL_FUNCTION 創(chuàng)建函數(shù)調(diào)用所需的空間。

也就是說,in 直接指向了最終的C層函數(shù),一步到位,也不走Python虛擬機的函數(shù)調(diào)用,而 __contains__ 調(diào)用方式先屬性查找和Python函數(shù)調(diào)用的開銷;所以 str.__contains__(other) 的形式要慢得多。

一般來說,in 方式更快只使用 Python 內(nèi)置的C實現(xiàn)的類。對于用戶自定義類,因為最終調(diào)用都是Python級的,所以兩種方式都要對函數(shù)調(diào)用所需的空間的。

find 與 index 的比較

find 與 index 的查找方式的區(qū)別僅僅只是 index 在子串不存在時會拋出異常。從源碼來看:

static PyObject *
unicode_find(PyObject *self, PyObject *args)
{
  /* initialize variables to prevent gcc warning */
  PyObject *substring = NULL;
  Py_ssize_t start = 0;
  Py_ssize_t end = 0;
  Py_ssize_t result;

  if (!parse_args_finds_unicode("find", args, &substring, &start, &end))
    return NULL;

  if (PyUnicode_READY(self) == -1)
    return NULL;

  result = any_find_slice(self, substring, start, end, 1);

  if (result == -2)
    return NULL;

  return PyLong_FromSsize_t(result);
}

static PyObject *
unicode_index(PyObject *self, PyObject *args)
{
  /* initialize variables to prevent gcc warning */
  Py_ssize_t result;
  PyObject *substring = NULL;
  Py_ssize_t start = 0;
  Py_ssize_t end = 0;

  if (!parse_args_finds_unicode("index", args, &substring, &start, &end))
    return NULL;

  if (PyUnicode_READY(self) == -1)
    return NULL;

  result = any_find_slice(self, substring, start, end, 1);

  if (result == -2)
    return NULL;

  if (result < 0) {
    PyErr_SetString(PyExc_ValueError, "substring not found");
    return NULL;
  }

  return PyLong_FromSsize_t(result);
}

實現(xiàn)方式基本相同,所以在子串存在的時候,兩者的性能一致;而當子串不存在時,index 會設置異常,因此涉及異常棧的空間等異常機制,速度上也就慢了一些。

總結(jié)

in 的搜索方式性能最佳,可讀性也最好,屬最佳實踐。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。

擴展閱讀

https://stackoverflow.com/questions/38400370/why-in-is-faster-than-contains

相關文章

最新評論