python一个优秀的时间函数,可以得到平均时间和剩余时间

上代码

import time
import datetime

class Timer(object):
	def __init__(self):
		self.init_time = time.time()
		self.total_time = 0.
		self.calls = 0
		self.start_time = 0.
		self.diff = 0.
		self.average_time = 0.
		self.remain_time = 0.

	def tic(self):#实现时间初始化
		self.start_time = time.time()

	def toc(self, average = True):#计算平均时间
		self.diff = time.time() - self.start_time
		self.total_time += self.diff
		self.call +=1
		self.average_time = self.total_time / self.calls
		if average:
			return self.average_time
		else:
			return self.diff

	def remain(self,iters,max_iters): # 剩余多长时间
		if iters == 0:
			self.remain_time = 0
		else:
			self.remain_time = (time.time() - self.init_time) / iters \ * (max_iters - iters)
		return str(datetime.timedelta(seconds = int(self.remain_time))) #转化为时分秒的格式

t = Timer()
t.tic()
time.sleep(3)
print(t.toc()) #3.00016450881958
a = t.remain(1,3)
print("remain time:%s"%a) #remain time:0:00:06

具体例子见代码注释。
下一篇见详细介绍yolo算法读取数据集及标签的代码

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
时间片轮转调度算法是一种常见的进程调度算法,Python可以通过模拟实现时间片轮转调度算法。具体实现步骤如下: 1. 定义进程类,包括进程ID、到达时间、服务时间、剩余服务时间、开始时间、结束时间、周转时间、带权周转时间等属性。 2. 定义时间片轮转调度函数,该函数接收一个进程列表和时间片大小作为参数,模拟时间片轮转调度算法的执行过程。 3. 在时间片轮转调度函数中,首先按照到达时间对进程列表进行排序,然后定义一个就绪队列和一个当前进程变量,将就绪队列初始化为空,当前进程变量初始化为None。 4. 在每个时间片中,遍历进程列表,将到达时间小于等于当前时间且剩余服务时间大于0的进程加入就绪队列。 5. 如果当前进程变量为空且就绪队列不为空,则从就绪队列中取出第一个进程作为当前进程,并将其开始时间设置为当前时间。 6. 如果当前进程变量不为空,则将其剩余服务时间减去一个时间片的大小,如果剩余服务时间小于等于0,则将其结束时间设置为当前时间,并从进程列表中移除该进程。 7. 将当前时间加上一个时间片的大小,继续执行下一个时间片,直到所有进程都执行完毕。 下面是Python实现时间片轮转调度算法的代码示例: ```python class Process: def __init__(self, pid, arrive_time, service_time): self.pid = pid self.arrive_time = arrive_time self.service_time = service_time self.remain_time = service_time self.start_time = 0 self.end_time = 0 self.turnaround_time = 0 self.weighted_turnaround_time = 0 def round_robin(processes, quantum): processes.sort(key=lambda x: x.arrive_time) # 按到达时间排序 ready_queue = [] current_process = None current_time = 0 while processes or ready_queue or current_process: # 将到达时间小于等于当前时间且剩余服务时间大于0的进程加入就绪队列 while processes and processes[0].arrive_time <= current_time: ready_queue.append(processes.pop(0)) # 如果当前进程为空且就绪队列不为空,则从就绪队列中取出第一个进程作为当前进程 if not current_process and ready_queue: current_process = ready_queue.pop(0) current_process.start_time = current_time # 将当前进程剩余服务时间减去一个时间片的大小 if current_process: current_process.remain_time -= quantum # 如果剩余服务时间小于等于0,则将其结束时间设置为当前时间,并从进程列表中移除该进程 if current_process.remain_time <= 0: current_time += quantum + current_process.remain_time current_process.end_time = current_time current_process.turnaround_time = current_process.end_time - current_process.arrive_time current_process.weighted_turnaround_time = current_process.turnaround_time / current_process.service_time current_process = None # 如果剩余服务时间大于0,则将其放回就绪队列的末尾 else: current_time += quantum ready_queue.append(current_process) current_process = None # 如果当前进程为空且就绪队列也为空,则将当前时间设置为下一个进程的到达时间 else: current_time = processes[0].arrive_time # 计算平均周转时间平均带权周转时间 total_turnaround_time = sum(p.turnaround_time for p in processes) total_weighted_turnaround_time = sum(p.weighted_turnaround_time for p in processes) avg_turnaround_time = total_turnaround_time / len(processes) avg_weighted_turnaround_time = total_weighted_turnaround_time / len(processes) print("Average turnaround time:", avg_turnaround_time) print("Average weighted turnaround time:", avg_weighted_turnaround_time) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值