python定时执行指定函数的方法
作者:依山带水 时间:2021-04-09 03:20:14
本文实例讲述了python定时执行指定函数的方法。分享给大家供大家参考。具体实现方法如下:
# time a function using time.time() and the a @ function decorator
# tested with Python24 vegaseat 21aug2005
import time
def print_timing(func):
def wrapper(*arg):
t1 = time.time()
res = func(*arg)
t2 = time.time()
print '%s took %0.3f ms' % (func.func_name, (t2-t1)*1000.0)
return res
return wrapper
# declare the @ decorator just before the function, invokes print_timing()
@print_timing
def getPrimeList(n):
""" returns a list of prime numbers from 2 to < n using a sieve algorithm"""
if n < 2: return []
if n == 2: return [2]
# do only odd numbers starting at 3
s = range(3, n+1, 2)
# n**0.5 may be slightly faster than math.sqrt(n)
mroot = n ** 0.5
half = len(s)
i = 0
m = 3
while m <= mroot:
if s[i]:
j = (m*m-3)//2
s[j] = 0
while j < half:
s[j] = 0
j += m
i = i+1
m = 2*i+3
return [2]+[x for x in s if x]
if __name__ == "__main__":
print "prime numbers from 2 to <10,000,000 using a sieve algorithm"
primeList = getPrimeList(10000000)
time.sleep(2.5)
"""
my output -->
prime numbers from 2 to <10,000,000 using a sieve algorithm
getPrimeList took 4750.000 ms
"""
希望本文所述对大家的Python程序设计有所帮助。
标签:python,定时,执行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
文字的减法
2007-11-06 12:58:00
python实现周期方波信号频谱图
2021-04-11 00:11:46
![](https://img.aspxhome.com/file/2023/5/91935_0s.png)
button标签的type属性默认值是?
2010-08-16 12:33:00
Django中外键ForeignKey介绍使用
2023-05-18 01:26:30
python使用pyodbc连接sqlserver
2021-12-24 06:06:17
![](https://img.aspxhome.com/file/2023/1/69031_0s.png)
Python详解复杂CSV文件处理方法
2021-04-05 11:12:02
![](https://img.aspxhome.com/file/2023/6/67936_0s.png)
关于浮动的前世今生
2009-08-19 18:51:00
![](https://img.aspxhome.com/file/UploadPic/20098/19/01-12s.png)
Python实现统计文本文件字数的方法
2023-05-24 11:46:04
Python实现异步IO的示例
2021-01-11 13:25:38
![](https://img.aspxhome.com/file/2023/4/102964_0s.jpg)
Django接收照片储存文件的实例代码
2022-06-01 09:05:29
Python爬虫之Scrapy环境搭建案例教程
2023-05-29 19:07:49
Python matplotlib超详细教程实现图形绘制
2023-04-20 06:37:53
![](https://img.aspxhome.com/file/2023/7/81657_0s.jpg)
解决Python selenium get页面很慢时的问题
2022-08-29 02:08:22
![](https://img.aspxhome.com/file/2023/6/90596_0s.jpg)
Python爬虫获取豆瓣电影并写入excel
2022-04-24 06:41:12
![](https://img.aspxhome.com/file/2023/4/76724_0s.png)
腾讯网QQ首页诞生的艰辛历程
2008-11-06 11:05:00
![](https://img.aspxhome.com/file/UploadPic/200811/6/01-77s.jpg)
Python深度学习pytorch实现图像分类数据集
2023-07-05 01:50:58
![](https://img.aspxhome.com/file/2023/2/99662_0s.png)
Python中元组的概念及应用小结
2022-03-28 15:01:05
![](https://img.aspxhome.com/file/2023/1/93491_0s.png)
微软工程师讲解SQL server阻塞
2008-01-05 14:02:00
如何在Python中将字符串转换为集合
2023-08-26 07:31:11
![](https://img.aspxhome.com/file/2023/0/62850_0s.png)
Python中xml和dict格式转换的示例代码
2021-07-18 21:30:10