osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

on a very slow function


def make_sequence_non_recursive(N, x0 = 2, c = -1):
  "What's wrong with this function?  It's very slow."
  last = x0
  def sequence():
    nonlocal last
    next = last
    last = last**2 + c
    return next % N
  return sequence

It crawls pretty soon.  Please advise?  Thank you.

>>> f = make_sequence_non_recursive(1032)
>>> timeit("f()", setup="from __main__ import f", number=1)
2.851021349670191e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
2.851021349670191e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
2.851021349670191e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
3.1361234960058937e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
3.1361234960058937e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
3.9914298213261645e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
4.276531967661867e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
4.276531967661867e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
4.846736260333273e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
5.131838292982138e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
7.412655463667761e-06
>>> timeit("f()", setup="from __main__ import f", number=1)
1.3684902228305873e-05
>>> timeit("f()", setup="from __main__ import f", number=1)
3.250164286328072e-05
>>> timeit("f()", setup="from __main__ import f", number=1)
9.009227323986124e-05
>>> timeit("f()", setup="from __main__ import f", number=1)
0.0002594429389546349
>>> timeit("f()", setup="from __main__ import f", number=1)
0.0007706310592539012
>>> timeit("f()", setup="from __main__ import f", number=1)
0.002319305833339058
>>> timeit("f()", setup="from __main__ import f", number=1)
0.007012372007011436
>>> timeit("f()", setup="from __main__ import f", number=1)
0.021163131162552418
>>> timeit("f()", setup="from __main__ import f", number=1)
0.06563849334929728
>>> timeit("f()", setup="from __main__ import f", number=1)
0.1944633166216363
>>> timeit("f()", setup="from __main__ import f", number=1)
0.5879943492758457
>>> timeit("f()", setup="from __main__ import f", number=1)
1.7068785165565714
>>> timeit("f()", setup="from __main__ import f", number=1)
5.1329479702866365
>>> timeit("f()", setup="from __main__ import f", number=1)
15.436177179570109
>>>