#23399: Update int_to_base36 -------------------------------------+------------------------------------- Reporter: liminspace | Owner: Type: | liminspace Cleanup/optimization | Status: new Component: Utilities | Version: master Severity: Normal | Resolution: Keywords: | Triage Stage: Has patch: 0 | Unreviewed Needs tests: 0 | Needs documentation: 0 Easy pickings: 0 | Patch needs improvement: 0 | UI/UX: 0 -------------------------------------+-------------------------------------
Comment (by kezabelle): It's an interesting idea. For the purposes of demonstration, here's some back-of-the-napkin unscientific benchmarks, from Python 2.7.6, with sys.maxint = 9223372036854775807: ---- > time python baseline.py {{{ import sys print("sys.maxint is: %s" % sys.maxint) for x in xrange(0, 1000000): continue print("Complete") }}} Consistently yields something about the range: > real 0m0.080s > user 0m0.066s > sys 0m0.012s ---- the version in django: > time python original.py {{{ import sys from django.utils.http import int_to_base36 print("sys.maxint is: %s" % sys.maxint) for x in xrange(0, 1000000): int_to_base36(i=x) print("Complete") }}} yields: > real 0m3.337s > user 0m3.314s > sys 0m0.020s ---- The version proposed by liminspace, adjusted to raise the same TypeErrors as Django's existing one: {{{ import sys from django.utils import six print("sys.maxint is: %s" % sys.maxint) def int_to_base36(n): if n < 0: raise ValueError("Negative base36 conversion input.") if six.PY2: if not isinstance(n, six.integer_types): raise TypeError("Non-integer base36 conversion input.") if n > sys.maxint: raise ValueError("Base36 conversion input too large.") c = '0123456789abcdefghijklmnopqrstuvwxyz' sign = '' if n < 0: sign, n = '-', -n if 0 <= n < 36: return sign + c[n] b36 = '' while n != 0: n, i = divmod(n, 36) b36 = c[i] + b36 return sign + b36 for x in xrange(0, 1000000): int_to_base36(n=x) print("Complete") }}} yields around: > real 0m2.859s > user 0m2.847s > sys 0m0.010s ---- So, over the first million smallest integers, there is some small speed-up (takes about [http://www.wolframalpha.com/input/?i=2.859+as+a+percent+of+3.337 85% of the time]), by the look of those numbers. For the top million (changed to {{{xrange(maxint - 1000000, 100000)}}}, the times seem a little more pronounced (the new version is [http://www.wolframalpha.com/input/?i=6.897+as+a+percent+of+11.840 about 60% of the original's time]): > time python baseline.py > real 0m0.069s > user 0m0.060s > sys 0m0.008s Django's original: > time python original.py > real 0m11.840s > user 0m11.805s > sys 0m0.029s The new version I pasted above: > time python new.py > real 0m6.897s > user 0m6.877s > sys 0m0.017s -- Ticket URL: <https://code.djangoproject.com/ticket/23399#comment:2> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/068.be8633292b818dc7aaffebcec6bda75c%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.