English 中文(简体)
1. 尝试多重复与选择
原标题:String multiplication versus for loop
  • 时间:2012-05-02 17:02:43
  •  标签:
  • python

我正在解决一个关于CodingBat.com的“灰色问题”。 我写了以下文字,涉及一个简单的问题,即印刷一个插曲的黑体。

def string_times(str, n):
    return n * str

正式结果:

def string_times(str, n):
    result = ""
    for i in range(n):
       result = result + str
    return result

print string_times( hello ,3)

这两项职能的产出相同。 我很奇怪的是,在业绩的基础上,扼杀多重复(第一功能)是如何针对休闲(第二功能)的。 我指的是什么是更快的,大多数是使用?

并请我提出自己回答这个问题的办法(利用时间(clock()或类似情况)

最佳回答

We can use the timeit module to test this:

python -m timeit "100* string "
1000000 loops, best of 3: 0.222 usec per loop

python -m timeit "  .join([ string  for _ in range(100)])"
100000 loops, best of 3: 6.9 usec per loop

python -m timeit "result =   " "for i in range(100):" "  result = result +  string "
100000 loops, best of 3: 13.1 usec per loop

你们可以看到,倍增是更为快捷的选择。 阁下可以注意到,虽然扼杀的分类版本是CPython的不正确版本,但的其他版本中可能并不适用。 因此,你应始终选择加强多重复或<代码>str.join(),这不仅只是为了速度,而且为了可读性和简洁性。

问题回答

我为以下三项职能提供了时间:

def string_times_1(s, n):
    return s * n

def string_times_2(s, n):
    result = ""
    for i in range(n):
       result = result + s
    return result

def string_times_3(s, n):
    "".join(s for _ in range(n))

成果如下:

In [4]: %timeit string_times_1( hello , 10)
1000000 loops, best of 3: 262 ns per loop

In [5]: %timeit string_times_2( hello , 10)
1000000 loops, best of 3: 1.63 us per loop

In [6]: %timeit string_times_3( hello , 10)
100000 loops, best of 3: 3.87 us per loop

如你所知,<代码>* n不仅是最明确和最简明的,也是最快的。

http://docs.python.org/library/timeit.html 无论从指挥线还是从法典上看,都看一看 code法的某些范围是:

$ python -m timeit ""something" * 100"
1000000 loops, best of 3: 0.608 usec per loop

Do something similar for your other function and compare.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签