python定时执行指定函数的方法


Posted in Python onMay 27, 2015

本文实例讲述了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 相关文章推荐
爬山算法简介和Python实现实例
Apr 26 Python
Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例
Mar 15 Python
python+pandas分析nginx日志的实例
Apr 28 Python
python3.x实现发送邮件功能
May 22 Python
python 筛选数据集中列中value长度大于20的数据集方法
Jun 14 Python
python随机在一张图像上截取任意大小图片的方法
Jan 24 Python
Python任意字符串转16, 32, 64进制的方法
Jun 12 Python
简单介绍python封装的基本知识
Aug 10 Python
python标准库os库的函数介绍
Feb 12 Python
pytorch读取图像数据转成opencv格式实例
Jun 02 Python
了解一下python内建模块collections
Sep 07 Python
Django限制API访问频率常用方法解析
Oct 12 Python
python统计文本字符串里单词出现频率的方法
May 26 #Python
python通过get,post方式发送http请求和接收http响应的方法
May 26 #Python
python使用urllib2提交http post请求的方法
May 26 #Python
Python同时向控制台和文件输出日志logging的方法
May 26 #Python
python实现查找excel里某一列重复数据并且剔除后打印的方法
May 26 #Python
python使用正则表达式提取网页URL的方法
May 26 #Python
python获取指定路径下所有指定后缀文件的方法
May 26 #Python
You might like
php操作xml
2013/10/27 PHP
PHP远程连接oracle数据库操作实现方法图文详解
2019/04/11 PHP
枚举JavaScript对象的函数
2006/12/22 Javascript
基于jquery的页面划词搜索JS
2010/09/14 Javascript
基于BootStrap Metronic开发框架经验小结【五】Bootstrap File Input文件上传插件的用法详解
2016/05/12 Javascript
微信小程序 使用canvas制作K线实例详解
2017/01/12 Javascript
vue router 跳转后回到顶部的实例
2018/08/31 Javascript
微信小程序地图实现展示线路
2020/07/29 Javascript
在Django的视图中使用form对象的方法
2015/07/18 Python
利用PyInstaller将python程序.py转为.exe的方法详解
2017/05/03 Python
Python实现字典去除重复的方法示例
2017/07/31 Python
python pandas 对时间序列文件处理的实例
2018/06/22 Python
Python实现简单的用户交互方法详解
2018/09/25 Python
Django框架实现的简单分页功能示例
2018/12/04 Python
numpy:找到指定元素的索引示例
2019/11/26 Python
使用Jupyter notebooks上传文件夹或大量数据到服务器
2020/04/14 Python
Python延迟绑定问题原理及解决方案
2020/08/04 Python
Python爬虫如何破解JS加密的Cookie
2020/11/19 Python
分享unittest单元测试框架中几种常用的用例加载方法
2020/12/02 Python
Python用户自定义异常的实现
2020/12/25 Python
python自动生成sql语句的脚本
2021/02/24 Python
css3学习之2D转换功能详解
2016/12/23 HTML / CSS
Philosophy美国官网:美国美容品牌
2016/08/15 全球购物
加拿大在线旅游公司:Flighthub
2019/03/11 全球购物
武汉瑞得软件笔试题
2015/10/27 面试题
Laravel中Kafka的使用详解
2021/03/24 PHP
高中生自我鉴定范文
2013/10/30 职场文书
项目经理的岗位职责
2013/11/23 职场文书
施工安全协议书
2013/12/11 职场文书
巧克力蛋糕店创业计划书
2014/01/14 职场文书
防灾减灾日活动总结
2014/08/26 职场文书
学习朴航瑛老师爱岗敬业先进事迹思想汇报
2014/09/17 职场文书
教师师德师风个人整改方案
2014/09/18 职场文书
2015年入党积极分子评语
2015/03/26 职场文书
《女娲补天》读后感5篇
2019/12/31 职场文书
numpy数据类型dtype转换实现
2021/04/24 Python