• <legend id='63FNi'><style id='63FNi'><dir id='63FNi'><q id='63FNi'></q></dir></style></legend>
        <bdo id='63FNi'></bdo><ul id='63FNi'></ul>
    1. <i id='63FNi'><tr id='63FNi'><dt id='63FNi'><q id='63FNi'><span id='63FNi'><b id='63FNi'><form id='63FNi'><ins id='63FNi'></ins><ul id='63FNi'></ul><sub id='63FNi'></sub></form><legend id='63FNi'></legend><bdo id='63FNi'><pre id='63FNi'><center id='63FNi'></center></pre></bdo></b><th id='63FNi'></th></span></q></dt></tr></i><div id='63FNi'><tfoot id='63FNi'></tfoot><dl id='63FNi'><fieldset id='63FNi'></fieldset></dl></div>

    2. <small id='63FNi'></small><noframes id='63FNi'>

        <tfoot id='63FNi'></tfoot>
      1. 设置与frozenset 性能

        时间:2023-07-03

            <small id='ds5nL'></small><noframes id='ds5nL'>

              <tfoot id='ds5nL'></tfoot>
            • <i id='ds5nL'><tr id='ds5nL'><dt id='ds5nL'><q id='ds5nL'><span id='ds5nL'><b id='ds5nL'><form id='ds5nL'><ins id='ds5nL'></ins><ul id='ds5nL'></ul><sub id='ds5nL'></sub></form><legend id='ds5nL'></legend><bdo id='ds5nL'><pre id='ds5nL'><center id='ds5nL'></center></pre></bdo></b><th id='ds5nL'></th></span></q></dt></tr></i><div id='ds5nL'><tfoot id='ds5nL'></tfoot><dl id='ds5nL'><fieldset id='ds5nL'></fieldset></dl></div>

            • <legend id='ds5nL'><style id='ds5nL'><dir id='ds5nL'><q id='ds5nL'></q></dir></style></legend>

                  <tbody id='ds5nL'></tbody>

                  <bdo id='ds5nL'></bdo><ul id='ds5nL'></ul>
                • 本文介绍了设置与frozenset 性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在修改 Python 的 setfrozenset 集合类型.

                  I was tinkering around with Python's set and frozenset collection types.

                  最初,我认为 frozenset 会提供比 set 更好的查找性能,因为它是不可变的,因此可以利用存储项目的结构.

                  Initially, I assumed that frozenset would provide a better lookup performance than set, as its immutable and thus could exploit the structure of the stored items.

                  但是,关于以下实验,情况似乎并非如此:

                  However, this does not seem to be the case, regarding the following experiment:

                  import random
                  import time
                  import sys
                  
                  def main(n):
                      numbers = []
                      for _ in xrange(n):
                          numbers.append(random.randint(0, sys.maxint))
                      set_ = set(numbers)
                      frozenset_ = frozenset(set_)
                  
                      start = time.time()
                      for number in numbers:
                          number in set_
                      set_duration = time.time() - start
                  
                      start = time.time()
                      for number in numbers:
                          number in frozenset_
                      frozenset_duration = time.time() - start
                  
                      print "set      : %.3f" % set_duration
                      print "frozenset: %.3f" % frozenset_duration
                  
                  
                  if __name__ == "__main__":
                      n = int(sys.argv[1])
                      main(n)
                  

                  我使用 CPython 和 PyPy 执行了这段代码,结果如下:

                  I executed this code using both CPython and PyPy, which gave the following results:

                  > pypy set.py 100000000
                  set      : 6.156
                  frozenset: 6.166
                  
                  > python set.py 100000000
                  set      : 16.824
                  frozenset: 17.248
                  

                  似乎 frozenset 在 CPython 和 PyPy 中的查找性能实际上都比较慢.有人知道为什么会这样吗?我没有研究实现.

                  It seems that frozenset is actually slower regarding the lookup performance, both in CPython and in PyPy. Does anybody have an idea why this is the case? I did not look into the implementations.

                  推荐答案

                  frozensetset 实现在很大程度上是共享的;set 只是一个 frozenset 添加了变异方法,具有完全相同的哈希表实现.请参阅 Objects/setobject.c 源文件;顶级 PyFrozenSet_Type 定义 与PySet_Type 定义.

                  The frozenset and set implementations are largely shared; a set is simply a frozenset with mutating methods added, with the exact same hashtable implementation. See the Objects/setobject.c source file; the top-level PyFrozenSet_Type definition shares functions with the PySet_Type definition.

                  这里没有对冻结集进行优化,因为当您测试成员资格时,不需要计算 in frozenset 中的项目的哈希值.您用来针对集合测试的项目仍然需要计算其哈希值,以便在集合哈希表中找到正确的插槽,以便您可以进行相等性测试.

                  There is no optimisation for a frozenset here, as there is no need to calculate the hashes for the items in the frozenset when you are testing for membership. The item that you use to test against the set still needs to have their hash calculated, in order to find the right slot in the set hashtable so you can do an equality test.

                  因此,您的计时结果可能由于系统上正在运行的其他进程而关闭;你测量了挂钟时间,没有禁用 Python 垃圾收集,也没有重复测试相同的东西.

                  As such, your timing results are probably off due to other processes running on your system; you measured wall-clock time, and did not disable Python garbage collection nor did you repeatedly test the same thing.

                  尝试使用 timeit 模块运行您的测试,其中一个numbers 中的值和一个不在集合中的值:

                  Try to run your test using the timeit module, with one value from numbers and one not in the set:

                  import random
                  import sys
                  import timeit
                  
                  numbers = [random.randrange(sys.maxsize) for _ in range(10000)]
                  set_ = set(numbers)
                  fset = frozenset(numbers)
                  present = random.choice(numbers)
                  notpresent = -1
                  test = 'present in s; notpresent in s'
                  
                  settime = timeit.timeit(
                      test,
                      'from __main__ import set_ as s, present, notpresent')
                  fsettime = timeit.timeit(
                      test,
                      'from __main__ import fset as s, present, notpresent')
                  
                  print('set      : {:.3f} seconds'.format(settime))
                  print('frozenset: {:.3f} seconds'.format(fsettime))
                  

                  这将每个测试重复 100 万次并产生:

                  This repeats each test 1 million times and produces:

                  set      : 0.050 seconds
                  frozenset: 0.050 seconds
                  

                  这篇关于设置与frozenset 性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python 和 Numpy 的 nan 和 set 下一篇:python中的一组列表列表

                  相关文章

                  • <bdo id='JZBq8'></bdo><ul id='JZBq8'></ul>

                    <small id='JZBq8'></small><noframes id='JZBq8'>

                  1. <legend id='JZBq8'><style id='JZBq8'><dir id='JZBq8'><q id='JZBq8'></q></dir></style></legend>
                    1. <tfoot id='JZBq8'></tfoot>
                      <i id='JZBq8'><tr id='JZBq8'><dt id='JZBq8'><q id='JZBq8'><span id='JZBq8'><b id='JZBq8'><form id='JZBq8'><ins id='JZBq8'></ins><ul id='JZBq8'></ul><sub id='JZBq8'></sub></form><legend id='JZBq8'></legend><bdo id='JZBq8'><pre id='JZBq8'><center id='JZBq8'></center></pre></bdo></b><th id='JZBq8'></th></span></q></dt></tr></i><div id='JZBq8'><tfoot id='JZBq8'></tfoot><dl id='JZBq8'><fieldset id='JZBq8'></fieldset></dl></div>