Skip to content

Instantly share code, notes, and snippets.

@smithdc1
Created January 15, 2021 16:32
Show Gist options
  • Save smithdc1/4fb47038a8d34f14764c58e98b213e61 to your computer and use it in GitHub Desktop.
Save smithdc1/4fb47038a8d34f14764c58e98b213e61 to your computer and use it in GitHub Desktop.
Benchmark for DecimalField.to_python()
from django.conf.global_settings import INSTALLED_APPS
import django
from django.conf import settings
from django.forms import DecimalField
import pyperf
settings.configure(INSTALLED_APPS= ('__main__',))
django.setup()
def test(loops):
range_it = range(loops)
f = DecimalField(max_digits=2, decimal_places=2)
t0 = pyperf.perf_counter()
# repeat to reduce impact of for loop
for loop in range_it:
f.clean('.01 ')
f.clean('.01')
f.clean(' .01')
f.clean(' .01 ')
f.clean('.01 ')
f.clean('.01')
f.clean(' .01')
f.clean(' .01 ')
f.clean('.01 ')
f.clean('.01')
f.clean(' .01')
f.clean(' .01 ')
f.clean('.01 ')
f.clean('.01')
f.clean(' .01')
f.clean(' .01 ')
return pyperf.perf_counter() - t0
runner = pyperf.Runner()
runner.bench_time_func('Decimal Field', test)
@smithdc1
Copy link
Author

Before

Decimal Field: Mean +- std dev: 395 us +- 8 us

After

Decimal Field: Mean +- std dev: 384 us +- 8 us

c.3% faster

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment