Pyrex: step in for loop

L

Luis P. Mendes

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm trying to improve speed in a module and substituted the pythonic
'for in range()' for 'for i from min < i < max:'

But, I need to define a step for the i variable. How can I do it?

for example, how do I iterate through 80000 to 140000 with step 10000?

Luis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFClkGTHn4UHCY8rB8RAgZXAJ0XPg9IH0OU329FVX3o14QjNFXuXgCgm+UR
O0GpXmDpQr7Y7TgMsmVvZ6s=
=zZnm
-----END PGP SIGNATURE-----
 
M

Mike Meyer

Luis P. Mendes said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm trying to improve speed in a module and substituted the pythonic
'for in range()' for 'for i from min < i < max:'

But, I need to define a step for the i variable. How can I do it?

for example, how do I iterate through 80000 to 140000 with step 10000?

guru% pydoc range
Help on built-in function range in module __builtin__:

range(...)
range([start,] stop[, step]) -> list of integers

Return a list containing an arithmetic progression of integers.
range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
When step is given, it specifies the increment (or decrement).
For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!
These are exactly the valid indices for a list of 4 elements.

so it's

for i in range(80000, 140000, 10000): ...

<mike
 
L

Luis P. Mendes

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


| so it's
|
| for i in range(80000, 140000, 10000): ...
|
| <mike

For what I've read, for i in range is slower than the other for
construct used by Pyrex:

for i from iMin <= i < iMax:

My question had to do with this new expression. How can I introduce a
step there.

I am able to circumvent the problem passing a reduced range for i, let's
say iMin = 8, iMax = 14 when calling the pyrex module and then, after
the for statement:
i = i * 10000
and it works as it is intended to.


But, if there is a way to introduce the step in the 'for i from iMin <=
i < iMax:' expression, I would like to know.

Thank you for your reply.

Luis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFClkmlHn4UHCY8rB8RAlUqAKCxSEkEKVIcoshTwmL7GQNK6d/j0wCgoC67
jOhuXQpnDt23SEAM9huKTQA=
=8XO0
-----END PGP SIGNATURE-----
 
M

Mike Meyer

Luis P. Mendes said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


| so it's
|
| for i in range(80000, 140000, 10000): ...
|
| <mike

For what I've read, for i in range is slower than the other for
construct used by Pyrex:

for i from iMin <= i < iMax:

My question had to do with this new expression. How can I introduce a
step there.

My bad. I missed the "Pyrex" in the subject line.

Sorry,
<mike
 
G

Greg Ewing

Luis said:
I'm trying to improve speed in a module and substituted the pythonic
'for in range()' for 'for i from min < i < max:'

But, I need to define a step for the i variable. How can I do it?

If you want maximum clarity, I'd suggest using the for-loop
to iterate over a contiguous range of integers and an expression
that maps the loop variable to whatever you want.

If you want the maximum possible speed, it *may* be faster
to use a while loop instead and do your own index updating.
But profile to make sure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top