运行 每次在 python 中使用 time.time() 时正好持续 5 秒的函数
Run a function for a duration of 5 secs exactly every time using time.time() in python
我定义了一个函数记录,它将值附加到一个名为列表的列表,持续时间为 Time.Whenever 我第一次调用这个函数,它向列表附加了 19 个值,但是当我第二次调用它时时间它将 20 个值附加到列表,即使我两次使用的时间参数都是 1 sec.Is 有什么方法可以让我在多次调用该函数时没有将相同的值附加到列表?
import time
lists=[]
first_list=[]
second_list=[]
def record(lists,Time):
start_time=time.time()
print "start_time",start_time
print "time.time",time.time()
while(time.time()-start_time)<=Time:
lists.append(1)
print "list",lists
print "length of list after appending",len(lists)
record(first_list,0.05)#first time
print 1
time.sleep(5)
record(second_list,0.05) #second time
列表被附加的次数几乎是任意的,执行所需的确切时间主要受当时计算机上 运行 的影响。例如,如果您在第二次调用期间打开了 Internet 浏览器,则可能会减慢确切的执行时间。
在我的机器上,两次调用平均每次调用只附加了 4 个值,但有时是 5 个。
Is there any way that I could get same no of values get appended to a
list when I call the function multiple times?
是:不要根据执行所需的时间来确定追加的项目数,而是根据您要追加的次数来确定:
def record(list_to_append_to, times_to_append):
for _ in range(times_to_append):
list_to_append_to.append(1)
或者类似地你可以使用 .extend
:
def record(list_to_extend, extend_length):
list_to_extend.extend(1 for _ in range(extend_length)
做某事所花的时间是不固定的。它需要多少时间。但是您需要两者相同,所以只需检查两者即可使它们相同。此代码完成所需的工作量,然后调用 time.sleep() 直到剩余时间块用完。这比反复循环和检查 time() 要好得多,因为循环和检查时间会使 cpu 努力工作以不做任何事情。 time.sleep 专门用于避免使用 cpu.
import time
def record(mylist, duration, count):
start_time=time.time()
while len(mylist) < count:
mylist.append(1)
remaining_time = time.time() - start_time;
time.sleep(duration - remaining_time)
print "calling first pass"
start_time=time.time()
l1 = []
record(l1, 5, 100) #second time
actual_duration = time.time() - start_time
print "length is", len(l1), "actual time was", actual_duration
print "calling second pass"
start_time=time.time()
l2 = []
record(l2, 5, 100) #second time
actual_duration = time.time() - start_time
print "length is", len(l2), "actual time was", actual_duration
显然,如果您尝试在指定时间内完成超出您能力范围的事情,您将无法做到。如果发生这种情况,也许您可以 return 报错?如果你在函数中检查 time() 可以提前退出,但我不建议检查得太频繁。
我定义了一个函数记录,它将值附加到一个名为列表的列表,持续时间为 Time.Whenever 我第一次调用这个函数,它向列表附加了 19 个值,但是当我第二次调用它时时间它将 20 个值附加到列表,即使我两次使用的时间参数都是 1 sec.Is 有什么方法可以让我在多次调用该函数时没有将相同的值附加到列表?
import time
lists=[]
first_list=[]
second_list=[]
def record(lists,Time):
start_time=time.time()
print "start_time",start_time
print "time.time",time.time()
while(time.time()-start_time)<=Time:
lists.append(1)
print "list",lists
print "length of list after appending",len(lists)
record(first_list,0.05)#first time
print 1
time.sleep(5)
record(second_list,0.05) #second time
列表被附加的次数几乎是任意的,执行所需的确切时间主要受当时计算机上 运行 的影响。例如,如果您在第二次调用期间打开了 Internet 浏览器,则可能会减慢确切的执行时间。
在我的机器上,两次调用平均每次调用只附加了 4 个值,但有时是 5 个。
Is there any way that I could get same no of values get appended to a list when I call the function multiple times?
是:不要根据执行所需的时间来确定追加的项目数,而是根据您要追加的次数来确定:
def record(list_to_append_to, times_to_append):
for _ in range(times_to_append):
list_to_append_to.append(1)
或者类似地你可以使用 .extend
:
def record(list_to_extend, extend_length):
list_to_extend.extend(1 for _ in range(extend_length)
做某事所花的时间是不固定的。它需要多少时间。但是您需要两者相同,所以只需检查两者即可使它们相同。此代码完成所需的工作量,然后调用 time.sleep() 直到剩余时间块用完。这比反复循环和检查 time() 要好得多,因为循环和检查时间会使 cpu 努力工作以不做任何事情。 time.sleep 专门用于避免使用 cpu.
import time
def record(mylist, duration, count):
start_time=time.time()
while len(mylist) < count:
mylist.append(1)
remaining_time = time.time() - start_time;
time.sleep(duration - remaining_time)
print "calling first pass"
start_time=time.time()
l1 = []
record(l1, 5, 100) #second time
actual_duration = time.time() - start_time
print "length is", len(l1), "actual time was", actual_duration
print "calling second pass"
start_time=time.time()
l2 = []
record(l2, 5, 100) #second time
actual_duration = time.time() - start_time
print "length is", len(l2), "actual time was", actual_duration
显然,如果您尝试在指定时间内完成超出您能力范围的事情,您将无法做到。如果发生这种情况,也许您可以 return 报错?如果你在函数中检查 time() 可以提前退出,但我不建议检查得太频繁。