• <bdo id='UP6ia'></bdo><ul id='UP6ia'></ul>
    1. <small id='UP6ia'></small><noframes id='UP6ia'>

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

        哪个通常更快,产量或附加?

        时间:2023-09-02
          <bdo id='7umsY'></bdo><ul id='7umsY'></ul>

          <small id='7umsY'></small><noframes id='7umsY'>

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

          <tfoot id='7umsY'></tfoot>

            • <legend id='7umsY'><style id='7umsY'><dir id='7umsY'><q id='7umsY'></q></dir></style></legend>
                <tbody id='7umsY'></tbody>
                  本文介绍了哪个通常更快,产量或附加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我目前在一个个人学习项目中阅读 XML 数据库.我发现自己正在编写收集数据的函数,但我不确定返回它们的快速方法是什么.

                  I am currently in a personal learning project where I read in an XML database. I find myself writing functions that gather data and I'm not sure what would be a fast way to return them.

                  这通常更快:

                  1. yield
                  2. 函数中的几个 append() 然后 return 随后的 list?
                  1. yields, or
                  2. several append()s within the function then return the ensuing list?

                  我很高兴知道在什么情况下 yields 会比 append()s 更快,反之亦然.

                  I would be happy to know in what situations where yields would be faster than append()s or vice-versa.

                  推荐答案

                  yield 具有惰性的巨大优势,而且速度通常不是最好的 使用它的理由.但是,如果它在您的上下文中有效,那么没有理由不使用它:

                  yield has the huge advantage of being lazy and speed is usually not the best reason to use it. But if it works in your context, then there is no reason not to use it:

                  # yield_vs_append.py
                  data = range(1000)
                  
                  def yielding():
                      def yielder():
                          for d in data:
                              yield d
                      return list(yielder())
                  
                  def appending():
                      lst = []
                      for d in data:
                          lst.append(d)
                      return lst
                  

                  这是结果:

                  python2.7 -m timeit -s "from yield_vs_append import yielding,appending" "yielding()"
                  10000 loops, best of 3: 80.1 usec per loop
                  
                  python2.7 -m timeit -s "from yield_vs_append import yielding,appending" "appending()"
                  10000 loops, best of 3: 130 usec per loop
                  

                  至少在这个非常简单的测试中,yield 比 append 快.

                  At least in this very simple test, yield is faster than append.

                  这篇关于哪个通常更快,产量或附加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:返回自我python的目的 下一篇:Python `yield from`,还是返回一个生成器?

                  相关文章

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

                      <legend id='fjFPu'><style id='fjFPu'><dir id='fjFPu'><q id='fjFPu'></q></dir></style></legend>

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

                        <bdo id='fjFPu'></bdo><ul id='fjFPu'></ul>
                      <tfoot id='fjFPu'></tfoot>