如何在检查中断条件之前让循环运行一段时间? [英] How to let a loop run for a while before checking for break condition?

查看:59
本文介绍了如何在检查中断条件之前让循环运行一段时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有时事先知道,在循环中花费的时间将是几分钟甚至几小时的时间,因此优化每个
循环中的
元素使其运行得更快。

其中一条可以确保优化的指令是检查

休息条件,至少在知道

循环无法到达它的时间内。


任何想法如何编写这样的循环?


例如


counter = 2 * 64

在柜台时(但不要检查它第一个小时):

...做点什么......#并减少柜台


感谢任何提示,但特别是如果相关的话定时器在

我主要使用的Windows 2000 / XP系统上。


您如何看待这个想法?它有意义吗?


Claudio Grondi


Sometimes it is known in advance, that the time spent in a loop will be
in order of minutes or even hours, so it makes sense to optimize each
element in the loop to make it run faster.
One of instructions which can sure be optimized away is the check for
the break condition, at least within the time where it is known that the
loop will not reach it.

Any idea how to write such a loop?

e.g.

counter = 2*64

while counter(BUT DON''T CHECK IT THE FIRST ONE HOUR LONG):
... do something ... # and decrease the counter

Thanks for any hint, but in particular if related to timers on the
Windows 2000/XP system I am mainly working with.

What do you think about this idea? Does it make sense?

Claudio Grondi

推荐答案

Claudio Grondi schrieb:
Claudio Grondi schrieb:

>

有时事先知道,在一个循环中花费的时间将是几分钟甚至几分钟的b $ b小时,所以在循环中优化每个元素b b元素以使其运行更快是有意义的。

其中一条可以确保优化的指令是检查

休息条件,至少在知道

循环无法到达它的时间内。


任何想法如何写这样的循环?


例如


counter = 2 * 64


而柜台(但不要检查第一个小时):
>
Sometimes it is known in advance, that the time spent in a loop will be
in order of minutes or even hours, so it makes sense to optimize each
element in the loop to make it run faster.
One of instructions which can sure be optimized away is the check for
the break condition, at least within the time where it is known that the
loop will not reach it.

Any idea how to write such a loop?

e.g.

counter = 2*64

while counter(BUT DON''T CHECK IT THE FIRST ONE HOUR LONG):



now = time.time()

而time.time () - 现在< 3600.0或some_other_condition:

...

短路或将阻止执行

some_other_condition。

now = time.time()
while time.time() - now < 3600.0 or some_other_condition:
...
The short circuiting of the or will prevent the execution of
some_other_condition.


...做点什么......#并减少计数器


感谢任何提示,但特别是如果与

我主要使用Windows 2000 / XP系统。


您如何看待这个想法?是否有意义?
... do something ... # and decrease the counter

Thanks for any hint, but in particular if related to timers on the
Windows 2000/XP system I am mainly working with.

What do you think about this idea? Does it make sense?



有什么想法?


Diez


What idea?

Diez


Diez B. Roggisch写道:
Diez B. Roggisch wrote:

Claudio Grondi schrieb:
Claudio Grondi schrieb:

>>
有时它是事先已知,在循环中花费的时间将是几分钟甚至几小时,因此优化循环中的每个元素以使其运行更快是有意义的。
可以肯定地优化的说明之一是检查断裂条件,至少在已知环路不能到达它的时间内。

知道怎么写这样的循环吗?

柜台= 2 * 64

同时反击(但不要检查它第一个小时):
>>
Sometimes it is known in advance, that the time spent in a loop will
be in order of minutes or even hours, so it makes sense to optimize
each element in the loop to make it run faster.
One of instructions which can sure be optimized away is the check for
the break condition, at least within the time where it is known that
the loop will not reach it.

Any idea how to write such a loop?

e.g.

counter = 2*64

while counter(BUT DON''T CHECK IT THE FIRST ONE HOUR LONG):




now = time.time()

而time.time() - now< ; 3600.0或some_other_condition:

...


短路或将阻止执行

some_other_condition。 />



now = time.time()
while time.time() - now < 3600.0 or some_other_condition:
...
The short circuiting of the or will prevent the execution of
some_other_condition.


> ...做点什么......#并减少计数器

感谢任何提示,但特别是如果与我主要使用的Windows 2000 / XP系统上的计时器有关。

您如何看待这个想法?是否有意义?
> ... do something ... # and decrease the counter

Thanks for any hint, but in particular if related to timers on the
Windows 2000/XP system I am mainly working with.

What do you think about this idea? Does it make sense?



有什么想法?


What idea?



这个你从我写的东西中得不到的。

我想,介绍性的文字给出了足够的背景信息。能够看到我的意思,但我显然是错的。


想法是通过使用定时器中断干扰来加速循环带循环的
,这样只有在定时器中断发生后,

循环才会在每次迭代中开始检查其中断条件。

否检查循环中的任何类型都应该发生在那个时间之前

最小化循环内每次迭代的操作次数

本身(即循环或多或少赢了不知道,

有一个计时器,以便以后改变循环行为。)


我希望上面的内容有助于理解我想要实现的目标。


Claudio Grondi

This one you haven''t probably got from what I have written.
I thought, that the introductory text gives enough context to be able to
see what I mean, but I was apparently wrong.

The idea is to speed up a loop by using a timer interrupt interfering
with the loop, so that only after the timer interrupt would occur, the
loop will start to check its break condition in each iteration.
No checking of any kind in the loop should happen up to that time to
minimize the number of operations in each iteration within the loop
itself (i.e. the loop more or less won''t know, that there is a timer on
its way to change the loops behavior at a later time).

I hope this above helps to understand what I would like to achieve.

Claudio Grondi


这个想法是通过使用一个计时器来加速循环用循环干扰
The idea is to speed up a loop by using a timer interrupt interfering

,这样只有在定时器中断发生后,

循环才会开始检查其中断条件每次迭代。

在这个循环中不会检查任何类型的循环到

最小化循环内每次迭代的操作次数

本身(即这个循环或多或少都不知道,

有一个计时器,以便以后改变循环行为)。
with the loop, so that only after the timer interrupt would occur, the
loop will start to check its break condition in each iteration.
No checking of any kind in the loop should happen up to that time to
minimize the number of operations in each iteration within the loop
itself (i.e. the loop more or less won''t know, that there is a timer on
its way to change the loops behavior at a later time).



while循环有一个条件。期。唯一要改变的是

引入一个未编码的循环,并使用自修改代码使它成为你的定时器中断后循环的
a。 />

但是当然整个事情都没有实际意义 - 如果在你的应用程序需要这个级别的时候,你需要使用C或汇编来修改mu-sec $。 br />
Diez

A while loop has a condition. period. The only thing to change that is
to introduce a uncoditioned loop, and use self-modifying code to make it
a while-loop after that timer interrupt of yours.

But of course that whole thing is a moot point - if shaving mu-secs on
that level is needed for your application, use C or assembly instead.
Diez


这篇关于如何在检查中断条件之前让循环运行一段时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆