How to let a loop run for a while before checking for break condition?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • matt@vazor.com

    #16
    Re: How to let a loop run for a while before checking for breakcondition?

    IMHO you're better off avoiding all this... It makes the code
    unnecessarily complicated when you're not sure if this is a performance
    bottleneck or not...

    Common advice is to write the code as simple as possible first, then go
    back and optimize as needed using the profiler. This is where
    extending in C becomes useful, but don't go to that level if it isn't
    really needed.

    KISS -- Keep It Simple Stupid

    thx

    m

    Hendrik van Rooyen wrote:
    "Claudio Grondi" <claudio.grondi @freenet.dewrot e:
    >
    >
    | Diez B. Roggisch wrote:
    | Claudio Grondi schrieb:
    | >
    | >>
    | >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()
    | while time.time() - now < 3600.0 or some_other_cond ition:
    | ...
    | >
    | >
    | The short circuiting of the or will prevent the execution of
    | some_other_cond ition.
    | >
    | > ... 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?
    | 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
    >
    I don't think this is usefully possible in python - the problem is that you will
    simply replace one check - The expiry of the counter - with another - to see if
    the interrupt has occurred already -
    >
    That said - the way I would do it would be to do something like this (in
    horrible pseudo code):
    >
    loop_start:
    do_something()
    jump loop_start
    if counter end_value:
    break
    jump loop_start
    loop_end:
    >
    >
    Interrupt_routi ne:
    replace the first jump to loop_start with a bunch of no - ops
    return
    >
    I don't think you can do this in python - it involves altering the running
    loop - but hey maybe I can learn something here...
    >
    This example sort of exposes the break for what it is - a jump statement in
    disguise - "look you cant recognise me - I am wearing dark glasses" - and
    "continue" is exactly like that too - the only difference is that the one jumps
    to the end, and the other to the beginning of the loop...
    >
    - Hendrik

    Comment

    • Matimus

      #17
      Re: How to let a loop run for a while before checking for break condition?


      Claudio Grondi wrote:
      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
      Would simple loop unrolling help? I've never experimented with loop
      unrolling in python, but perhaps something like this would help. Also,
      it sort of depends upon the condition. Do you know how many times 'do
      something' will be performed before hand?

      do_something = compile("print 'do_something'" ,"loop.tmp","si ngle")
      loop_list = [do_something]*(loop_count)
      for x in loop_list: exec(x)

      Note that this offloads some work upfront but depending on what is
      being done it might still be quicker. It will take up more memory, but
      the list will only be full of references. I'm pretty sure that the for
      loop, in this case, will not be doing a check each iteration since it
      is operating on a list, but I'm not positive.

      Comment

      Working...