分为三种,无参无返回值,有参无返回值,有参有返回值
# import module your need
# 4 编写装饰器来实现,对目标函数进行装饰,计算函数的运行耗时,
# 分3种情况:目标函数无参数无返回值,目标函数有参数,目标函数有返回值;
# 三个目标函数分别为:
# A 打印输出20000之内的素数;
# B 计算整数2-10000之间的素数的个数;
# C 计算整数2-M之间的素数的个数;
import time
#无参无返回值
def docro(func):
def inner():
start_time=time.time()
func()
end_time=time.time()
print("运行时间为%s"%(end_time-start_time))
return inner
#有参数无返回值
def docro2(func):
def inner(a,b):
start_time = time.time()
func(a,b)
end_time = time.time()
print("运行时间为%s" % (end_time - start_time))
return inner
#有参有返回值
def docro3(func):
def inner(a, b):
start_time = time.time()
res=func(a, b)
end_time = time.time()
print("运行时间为%s" % (end_time - start_time))
return res
return inner
def judge(num):
if num > 2:
for i in range(2,num):
if num%i == 0:
return False
return True
elif num==2:
return True
elif num==1:
return False
@docro
def prime1():
for i in range(1,20000):
if judge(i):
print(i,end=" ")
@docro2
def prime2(num1,num2):
totalnum=0
for i in range(num1,num2):
if judge(i):
totalnum+=1
print("素数个数为:",totalnum)
@docro3
def prime3(num1,num2):
totalnum=0
for i in range(num1,num2):
if judge(i):
totalnum+=1
print("素数个数为:",totalnum)
return totalnum
# prime1()
# prime2(2,10000)
prime3(2,int(input("请输入M")))