简体   繁体   English

python执行时间和延迟

[英]python execution time and delay

in that code part, im trying to collect 100 data(in the for loop) and i want if the for loop execution last less then 1 second, wait for (1-execution time) sec. 在该代码部分中,我试图收集100个数据(在for循环中),我想如果for循环执行的持续时间少于1秒,请等待(1-执行时间)秒。 how can i do that ? 我怎样才能做到这一点 ?

thanks 谢谢

while(1):               
        temparray=array('i')
        fileName = 'interval' + str(initialfreq) + '.txt'
        temp_file = open(fileName, 'wb')
    for z in range(100):
            readoff = ser.readline()                
            temp_file.write(readoff)
            readoff=int(readoff)
            temparray.append(readoff)
    print('biten aralik: '+str(initialfreq))
    general_list.write('interval'+str(initialfreq)+":    "+str(mean(temparray))+'\n')
    initialfreq= initialfreq + 1

Before the for loop, get the current time, as t0 . for循环之前,获取当前时间t0

After the for loop, get the current time again, as t1 . for循环之后,再次获得当前时间,即t1

Then, if t1 - t0 < 1 , time.sleep(1 - (t1 - t0)) . 然后,如果t1 - t0 < 1 ,则time.sleep(1 - (t1 - t0))

There are a few different choices of time objects you can use. 您可以使用几种不同的时间对象选择。 datetime.datetime is the simplest (especially if you need to debug this later—print out a datetime and it's immediately readable to a human), if you don't need the highest precision. 如果您不需要最高精度,那么datetime.datetime是最简单的(特别是如果您以后需要调试时,请打印出datetime并且可以被人类立即读取)。 When you subtract two datetime objects, you get a timedelta object. 当减去两个datetime对象时,将得到一个timedelta对象。 So: 所以:

t0 = datetime.datetime.now()
for …
t1 = datetime.datetime.now()
td = (t1 - t0).total_seconds()
if td < 1:
    time.sleep(1 - td)

If you need better precision, there are functions in the time module that let you use better clocks that your platform supports, especially if you're on 3.3+. 如果需要更高的精度,那么time模块中的功能可以让您使用平台支持的更好的时钟,尤其是在3.3+上。 See the clock_gettime function in 3.3+: 请参阅3.3+中的clock_gettime函数:

t0 = time.clock_gettime(time.CLOCK_MONOTONIC)
for …
t1 = time.clock_gettime(time.CLOCK_MONOTONIC)
td = (t1 - t0) / time.clock_getres(time.CLOCK_MONOTONIC)
# same code as above

CLOCK_MONOTONIC may not be the best clock for your platform—eg, if you have CLOCK_HIGHRES or CLOCK_MONOTONIC_RAW they will almost always be better. CLOCK_MONOTONIC可能不是适合您平台的最佳时钟,例如,如果您具有CLOCK_HIGHRESCLOCK_MONOTONIC_RAW它们几乎总是会更好。 So, read the docs and then check what you have. 因此,请阅读文档,然后检查您所拥有的。

In earlier versions (including all 2.x versions), you will have to choose between clock , perf_counter , process_time , or time , which all have different tradeoffs, and the tradeoffs are even different on different platforms (and datetime.datetime will already be at least as good as time ), so no one can tell you "always use this one". 在早期版本(包括所有2.x版本)中,您将不得不在clockperf_counterprocess_timetime之间进行选择,它们都有不同的权衡,并且在不同平台上的权衡甚至也不同(并且datetime.datetime已经是至少与time一样好),因此没有人可以告诉您“始终使用此功能”。

import time

now = time.time()
future = now + 1
for z in range(100):
  readoff = ser.readline()                
  temp_file.write(readoff)
  readoff=int(readoff)
  temparray.append(readoff)
time_span = future - time.time()
if time_span > 0:
  print 'sleeping for %g' %(1-time_span)
  time.sleep(1-time_span)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM