I don't have a clue why is this happening. I was messing with some lists, and I needed a for
loop going from 0 to log(n, 2)
where n was the length of a list. But the code was amazingly slow, so after a bit a research I found that the problem is in the range generation. Sample code for demonstration:
n = len([1,2,3,4,5,6,7,8])
k = 8
timeit('range(log(n, 2))', number=2, repeat=3) # Test 1
timeit('range(log(k, 2))', number=2, repeat=3) # Test 2
The output
2 loops, best of 3: 2.2 s per loop
2 loops, best of 3: 3.46 µs per loop
The number of tests is low (I didn't want this to be running more than 10 minutes), but it already shows that range(log(n, 2))
is orders of magnitude slower than the counterpart using just the logarithm of an integer. This is really surprising and I don't have any clue on why is this happening. Maybe is a problem on my PC, maybe a Sage problem or a Python bug (I didn't try the same on Python).
Using xrange
instead of range
doesn't help either. Also, if you get the number with .n()
, test 1 runs at the same speed of 2.
Does anybody know what can be happening? Thanks!