Ticket #546 (reopened defect)

Opened 3 years ago

Last modified 17 months ago

range(a,b,c) not optimized for (at compile time) unknown step value

Reported by: robertwb Owned by: somebody
Priority: major Milestone: wishlist
Component: Optimization Keywords:
Cc:

Description

We optimized range(a) and range(a,b), why not this one?

Change History

  Changed 3 years ago by scoder

We do. You may just have hit one of these cases:

            if not isinstance(step.constant_result, (int, long)):
                # cannot determine step direction
                return node
            step_value = step.constant_result
            if step_value == 0:
                # will lead to an error elsewhere
                return node

Could you provide example code that doesn't work?

  Changed 3 years ago by robertwb

  • status changed from new to closed
  • resolution set to worksforme

Hmm... it was probably the first case there.

follow-up: ↓ 5   Changed 3 years ago by robertwb

  • status changed from closed to reopened
  • resolution worksforme deleted

Actually, we can turn that second version into a c loop, it will just have to have a test that depends on the (runtime) sign of the step.

  Changed 20 months ago by scoder

  • summary changed from range(a,b,c) not optimized to range(a,b,c) not optimized for (at compile time) unknown step value

in reply to: ↑ 3   Changed 17 months ago by mark

Replying to robertwb:

Actually, we can turn that second version into a c loop, it will just have to have a test that depends on the (runtime) sign of the step.

What prange does is simple arithmetic, i.e. compute the number of steps that will be taken and compute the index variable from that. That way you won't have to use 'if' and duplicate the loop body:

nsteps = (stop - start) / step; for (i = 0; i < nsteps; i++) { index = start + step * i; ... }

Note: See TracTickets for help on using tickets.