18

whileループ内でwhileループの時間を計り、実行にかかる合計時間を記録し、ループするたびに実行にかかる時間を記録しようとしています。可能であれば、コードを使用してこれを実現する方法が必要です。または、まだ知らない可能性のあるさまざまな概念を受け入れる方法が必要です。

import random
import time
import sys

def main():


    looperCPU = 500
    start = time.time()
    while (looperCPU != 0):
        #start = time.time()

        #this is the computation for 3 secs
        time.sleep(3)
        random_number = random.randint(0,1000)

        #Send to printer for processing
        #.75 secs to 4.75 secs to generate a pause before printing
        secondsPause = random.uniform(.75,4.75)


        #This is the printer function
        printerLooper = True
        while printerLooper == True :
            print("Sleeping for ", secondsPause, " seconds")
            print(random_number)
            printerLooper = False


        #
        print("total time taken this loop: ", time.time() - start)
        looperCPU -= 1    



main()

ループは時間を出力しますが、ネストされたwhileループのスリープ時間を考慮していないことは間違いありません。Pythonが両方のwhileループ、つまり実行する必要のあるすべてのループ(この場合は500)の時間を計測できるようにするにはどうすればよいですか?

4

4 に答える 4

24

最初のループの外側でstartを設定すると、whileループの実行にかかる時間が正しくないことが保証されます。それは言うようなものです:

program_starts = time.time()
while(True):
    now = time.time()
    print("It has been {0} seconds since the loop started".format(now - program_starts))

これは、時間の経過とともに終了時間が着実に増加している間、開始は実行時間全体にわたって静的なままであるためです。ループ内に開始時間を配置することにより、プログラムの実行に伴って開始時間が増加することを保証します。

while (looperCPU != 0):
    start_time = time.time()
    # Do some stuff
    while printerLooper == True :
        print("Sleeping for ", secondsPause, " seconds")
        print(random_number)
        printerLooper = False
    end_time = time.time()

    print("total time taken this loop: ", end_time - start_time)
于 2013-02-07T01:44:11.757 に答える
3

Pythonでは、このドキュメントを調べることができますhttps://docs.python.org/2/library/timeit.html
Stackoverflowの例:timeitモジュールの使用方法

import timeit
timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)

Jupyterノートブックでは、

%%timeit -n 100
import pandas as pd
s = pd.Series([101.00, 121.00])
summary = 0
for item in s:
    summary+=item
于 2017-02-12T06:16:21.950 に答える
1

正しい考えですが、タイミング関数の配置は少しずれています。ここで修正しました。

import random
import time import sys

def main():

    looperCPU = 500
    start = time.time()
    while (looperCPU != 0):
       #this is the computation for 3 secs
       time.sleep(3)
       random_number = random.randint(0,1000)

       #Send to printer for processing
       #.75 secs to 4.75 secs to generate a pause before printing
       secondsPause = random.uniform(.75,4.75)


       #This is the printer function
       printerLooper = True
       myStart = time.time()
       while printerLooper == True :
          print("Sleeping for ", secondsPause, " seconds")
          print(random_number)
          printerLooper = False
          print("total time taken this loop: ", time.time() - myStart)    

       looperCPU -= 1    
main()
于 2013-02-07T01:40:50.467 に答える
1

このpytictocを使用できます。pipまたはcondaを使用してインストールします。pytictocのドキュメントを読んでください。これは、matlabstictocのように機能します。基本的には、ticでタイマーを開始し、tocで終了します。そして、uはそれを直接印刷するか、新しい変数に保存することができます。

from pytictoc import TicToc
def main()
    t = TicToc()  # create instance of class
    start_time= t.tic() #start timer
    while ():  


    #end of while loop

    end_time = t.toc()
    print("end_time", end_time)
于 2018-11-15T13:30:13.727 に答える