在本文中,我们将带你了解在Python中获取计时器滴答在这篇文章中,同时我们还将给您一些技巧,以帮助您实现更有效的asp.net–在执行计时器滴答功能时避免回发、python-2.7–读取矩阵并在py
在本文中,我们将带你了解在Python中获取计时器滴答在这篇文章中,同时我们还将给您一些技巧,以帮助您实现更有效的asp.net – 在执行计时器滴答功能时避免回发、python-2.7 – 读取矩阵并在python中获取行名和列名、Python中的计时器对象、在Python中停止计时器后,计时器无法重新启动。
本文目录一览:- 在Python中获取计时器滴答
- asp.net – 在执行计时器滴答功能时避免回发
- python-2.7 – 读取矩阵并在python中获取行名和列名
- Python中的计时器对象
- 在Python中停止计时器后,计时器无法重新启动
在Python中获取计时器滴答
我只是想计时一段代码。伪代码如下所示:
start = get_ticks()do_long_code()print "It took " + (get_ticks() - start) + " seconds."
在Python中看起来如何?
更具体地说,如何获得自午夜以来的滴答声数量(或者Python组织该计时)?
答案1
小编典典在time
模块中,有两个计时功能:time
和clock
。time
如果您在乎的话,它会给您“隔离”时间。
但是,python文档说clock
应该将其用于基准测试。请注意,clock
在单独的系统中,其行为有所不同:
在MS Windows上,它使用Win32函数QueryPerformanceCounter(),“分辨率通常优于微秒”。它没有特殊含义,只是一个数字(它
clock
在您第一次在过程中调用时会开始计数)。#ms视窗t0 = time.clock()做点什么()t = time.clock()-t0#t是经过的墙壁秒数(浮点数)
在* nix上,
clock
报告CPU时间。现在,这是不同的,并且很可能是您想要的值,因为程序几乎从来不是唯一请求CPU时间的进程(即使您没有其他进程,内核也会不时地使用CPU时间)。因此,在基准测试代码时,该数字通常比墙时间(即time.time()-t0)小¹,它更有意义:#Linuxt0 = time.clock()做点什么()t = time.clock()-t0#t是经过的CPU秒数(浮点数)
除此之外,timeit模块还具有Timer
使用最佳功能进行基准测试的类。
¹除非线程受阻…
²Python≥3.3:有time.perf_counter()
和time.process_time()
。perf_counter
被timeit
模块使用。
asp.net – 在执行计时器滴答功能时避免回发
我还使用计时器滴答功能来刷新广告旋转器中的图像.
但是当计时器滴答功能刷新时,刷新整个页面,以便重新加载我在页面中声明的所有功能.
我需要只在广告 – 旋转器图像中的图像应该刷新整个页面.
我需要避免整个页面刷新.
请帮助我.
$(document).ready(function() { setInterval(function() { $('#<% = addrotat.ClientID %>').load(location.href + ' #<% = addrotat.ClientID %> ','' + Math.random() + ''); },5000); });
解决方法
<asp:UpdatePanel ID="up1" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="yourControl" EventName="yourControlEvent" /> </Triggers> <ContentTemplate> your content goes here </ContentTemplate> </asp:UpdatePanel>
你可以查看这个链接
update panel,with Triggers,
Updatepanel documentation
example for adrotator without refreshing the page
python-2.7 – 读取矩阵并在python中获取行名和列名
sample sample1 sample2 sample3 sample1 1 0.7 0.8 sample2 0.7 1 0.8 sample3 0.8 0.8 1
我想获取所有值为>的对0.8.例如:sample1,sample3 0.8 sample2,sample3 0.8等在一个大文件中.
当我使用csv.reader时,每一行都会转入一个列表并跟踪行名和列名会使程序变得狡猾.我想知道一种优雅的方式,比如使用numpy或pandas.
期望的输出:
sample1,sample3 0.8 sample2,sample3 0.8
1可以忽略,因为在相同的样本之间,它总是1.
解决方法
np.triu
屏蔽掉上三角形值:
In [11]: df Out[11]: sample1 sample2 sample3 sample sample1 1.0 0.7 0.8 sample2 0.7 1.0 0.8 sample3 0.8 0.8 1.0 In [12]: np.triu(df,1) Out[12]: array([[ 0.,0.7,0.8],[ 0.,0.,0. ]]) In [13]: np.triu(df,1) >= 0.8 Out[13]: array([[False,False,True],[False,False]],dtype=bool)
然后提取它是真的索引/列我认为你必须使用np.where *:
In [14]: np.where(np.triu(df,1) >= 0.8) Out[14]: (array([0,1]),array([2,2]))
这为您提供了第一个索引索引和列索引的数组(这是这个numpy版本中效率最低的部分):
In [16]: index,cols = np.where(np.triu(df,1) >= 0.8) In [17]: [(df.index[i],df.columns[j],df.iloc[i,j]) for i,j in zip(index,cols)] Out[17]: [('sample1','sample3',0.80000000000000004),('sample2',0.80000000000000004)]
如预期的.
*我可能忘记了获取最后一个块的更简单方法(编辑:下面的pandas代码可以做到,但我认为可能还有其他方法.)
您可以在pandas中使用相同的技巧,但使用stack来本机获取索引/列:
In [21]: (np.triu(df,1) >= 0.8) * df Out[21]: sample1 sample2 sample3 sample sample1 0 0 0.8 sample2 0 0 0.8 sample3 0 0 0.0 In [22]: res = ((np.triu(df,1) >= 0.8) * df).stack() In [23]: res Out[23]: sample sample1 sample1 0.0 sample2 0.0 sample3 0.8 sample2 sample1 0.0 sample2 0.0 sample3 0.8 sample3 sample1 0.0 sample2 0.0 sample3 0.0 dtype: float64 In [24]: res[res!=0] Out[24]: sample sample1 sample3 0.8 sample2 sample3 0.8 dtype: float64
Python中的计时器对象
计时器对象用于特定时间运行的操作。往往被安排到特定的单独的线程上运行,
但是计时器初始化的时间间隔可能不是解释器实际执行操作时的实际时刻,
因为线程调度程序负责实际调度与计时器对象相对应的线程。
Timer是Thread类的子类。显式调用与计时器对应的start()函数来启动。
创建一个定时器语法
threading.Timer(interval,function,args = None,kwargs = None)
经过时间多少秒,后运行程序。如果args为None(默认值),则将使用空列表。如果kwargs为None(默认值),则将使用空的dict。
import threading
def fun():
print("hello\n")
timer = threading.Timer(1.0, fun)
timer.start()
print("quit\n")
程序start()运行5秒中后调用fun(),
timer.cancel() 停止计时器,取消执行计时器的操作。这仅在定时器仍处于等待阶段时才有效。
import threading
def fun():
print("hello\n")
timer = threading.Timer(1.0, fun)
timer.start()
print("Cancelling timer\n")
timer.cancel()
print("Exit\n")
使用threading.Timer实现线程循环任务定时器
import threading
def func1(a):
print(''TO DO 。。。'')
a+=1
print(a)
print(''当前激活线程数为'',threading.activeCount())
if a>5:
return
timer = threading.Timer(1, func1,(a,)) #1秒后调func1 不会造成线程堆积
timer.start()
timer = threading.Timer(5, func1,(0,)) #5秒后调用func1
timer.start()
运行结果
在Python中停止计时器后,计时器无法重新启动
我正在使用Python
2.7。我有一个计时器,它会不断重复计时器回调操作,直到停止为止。它使用一个Timer对象。问题是它停止后无法重新启动。Timer对象代码如下:
from threading import Timer
class RepeatingTimer(object):
def __init__(self,interval,function,*args,**kwargs):
super(RepeatingTimer,self).__init__()
self.args = args
self.kwargs = kwargs
self.function = function
self.interval = interval
def start(self):
self.callback()
def stop(self):
self.interval = False
def callback(self):
if self.interval:
self.function(*self.args,**self.kwargs)
Timer(self.interval,self.callback,).start()
要启动计时器,请运行以下代码;
repeat_timer = RepeatingTimer(interval_timer_sec,timer_function,arg1,arg2)
repeat_timer.start()
要停止计时器,代码为:
repeat_timer.stop()
停止后,我尝试通过调用重新启动计时器,repeat_timer.start()
但计时器无法启动。计时器停止后如何重新启动?
谢谢。
关于在Python中获取计时器滴答的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于asp.net – 在执行计时器滴答功能时避免回发、python-2.7 – 读取矩阵并在python中获取行名和列名、Python中的计时器对象、在Python中停止计时器后,计时器无法重新启动的相关知识,请在本站寻找。
本文标签: