Opened 8 years ago

Closed 8 years ago

#25743 closed Cleanup/optimization (fixed)

Optimize formats.localize(_input)

Reported by: Jaap Roes Owned by: nobody
Component: Utilities Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

While doing some benchmarking on some simple templates I noticed render_value_in_context was spending a disproportionate amount of time in utils.formats.localize while rendering a simple string.

Performance increased greatly on my machine after simply altering localize to bail early on strings. (I did the same thing for localize_input where I saw the same pattern.)

Here's a benchmark script and my results before and after:

import timeit

print('localize(string): %s' % timeit.repeat('localize("a")', setup='from django.utils.formats import localize'))
print('localize_input(string): %s' % timeit.repeat('localize_input("a")', setup='from django.utils.formats import localize_input'))

Before:

localize(string): [1.886936140996113, 1.960805894996156, 1.9073195710006985]
localize_input(string): [1.6485704379956587, 1.6588637870008824, 1.6481186080054613]

After:

localize(string): [0.2906172150032944, 0.2897624880060903, 0.28507295199960936]
localize_input(string): [0.28498719700291986, 0.2985827610027627, 0.2751107259973651]

Change History (2)

comment:1 by Tim Graham, 8 years ago

Component: Template systemUtilities
Triage Stage: UnreviewedReady for checkin

comment:2 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: newclosed

In 9a2aca60:

Fixed #25743 -- Optimized utils.localize() and localize_input()

Bail early if the input is a string since that's the most common case.

Note: See TracTickets for help on using tickets.
Back to Top