如何计算Python

时间:2015-07-31 17:43:16

标签: python

我有以下代码:

def base(nomor)
    day = localtime.tm_wday
    time = localtime.tm_hour
    no = str(nomor)
    dosen = cek_dosen(no)
    if dosen == 'null':
        no_dosen()
    elif dosen != 'null':
        ada_dosen()
        matkul = cek_jadwal(day,time,dosen)
        if matkul == 'null':
            no_jadwal()
        elif matkul != 'null':
            ada_jadwal()
            pertemuan = cek_pertemuan(matkul)
            print pertemuan
            if pertemuan > 1:
                cek_before(pertemuan)
                filename = ''.join([dosen, matkul, str(pertemuan), ".pptx"])
            else:
                filename = ''.join([dosen, matkul, str(pertemuan), ".pptx"])
            grabfile(filename)
            os.system(''.join(["loimpress ",filename]))
            pertemuan = pertemuan + 1
            update_pertemuan(pertemuan,matkul)


    mulai()


if __name__ == '__main__':
  mulai()
  while True: 
    data = port.read()
    count += 1
    if count == 1:
        if str(data) != start:
            nomor = ''
            count = 0
    elif 2 <= count <= 13:
        nomor = nomor + str(data)
    elif count == 16 and str(data) == stop:
        base(nomor)
        nomor = ''
        count = 0

我想计算从data = port.read()之后到grabfile(filename)之后的时间。我在start = time.time()data = port.read end = time.time()之后使用了grabfiletime = end - start之后使用了data = port.read(),但是在start = time.time()之后它被卡住了所以我使用Ctrl + C来不要那么做。如果我在no = str(nomor)之后添加Attribute Error : 'int' object has no attribute 'time',我会获得input[type="file"]

如何计算经过的时间?

3 个答案:

答案 0 :(得分:1)

from time import clock

start = clock()
...
print "Time taken = %.5f" % (clock() - start)

答案 1 :(得分:0)

<强>总结

import datetime

if __name__ == "__main__":
    d1 = datetime.datetime.now()
    data = port.read()
    # Do more things ...
    tdelta = datetime.datetime.now() - d1
    print(tdelta.total_seconds()) # This is your answer

答案 2 :(得分:-1)

看一下python timeit module

基本示例:

na.locf