Python 2 times slower than Perl
Skip Montanaro
skip at pobox.com
Wed Jul 18 11:46:59 EDT 2001
>> The revised stupid testing py is:
>> #!/usr/bin/env python
>> def f():
>> for i in xrange(2.5, 1e7, 1.):
>> j = 2.5 * 2.5
>> print i, j
>> f()
Note that the end condition for this loop is not quite the same as your
original while loop version. Before, the loop was
i = 2.5
while i < 1e7:
j = 2.5 * 2.5
i += 1
which resulted in a final value for i of 10000000.5.
The xrange fuction should be called as
xrange(2.5, 1e7+1.0, 1.0)
so that i terminates the loop with a value >= 1e7. Even so, note that both
the range and xrange functions coerce their arguments to ints:
>>> range(2.5, 1e2, 1.0)
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> xrange(2.5, 1e2, 1.0)[-1]
99
so at the end of
for i n xrange(2.5, 1e7+1, 1.):
pass
i has an integer value of 10000000, not a float value of 10000000.5, as was
the case for the while loop version.
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list