2

Python の timeit モジュールについて質問があります。これは、コードの実行にかかる時間を決定するために使用されます。

t = timeit.Timer("foo","from __main__ import foo")
str(t.timeit(1000))

上記のコードの引数 1000 の意味は何ですか?

4

3 に答える 3

4

ドキュメントから:

Timer.timeit([number=1000000])

numberメインステートメントの実行時間。これにより、セットアップステートメントが1回実行され、メインステートメントの実行にかかる時間が秒単位でfloatとして返されます。引数はループを通過する回数であり、デフォルトは100万回です。mainステートメント、setupステートメント、および使用されるタイマー関数がコンストラクターに渡されます。

于 2011-08-08T19:44:14.287 に答える
1

文書化されているように、この数字は、指定されたプログラムを実行する回数を示します。

最初の数回の実行はキャッシングのために大幅に遅くなる可能性があり、個々の実行時間は大きく異なる可能性があるため、タイミングの実行が多いほど(つまり、値が高いほど)、より正確な結果が得られますが、時間がかかります。

于 2011-08-08T19:44:13.200 に答える
1

男性に魚を教えるという精神で、Pythonに聞いてみてください。

>>> import timeit
>>> t=timeit.Timer()
>>> help(t.timeit)
Help on method timeit in module timeit:

timeit(self, number=1000000) method of timeit.Timer instance
    Time 'number' executions of the main statement.

    To be precise, this executes the setup statement once, and
    then returns the time it takes to execute the main statement
    a number of times, as a float measured in seconds.  The
    argument is the number of times through the loop, defaulting
    to one million.  The main statement, the setup statement and
    the timer function to be used are passed to the constructor.
于 2011-08-08T19:44:37.753 に答える