Skip to content

Instantly share code, notes, and snippets.

@smithdc1
Created January 15, 2021 13:15
Show Gist options
  • Save smithdc1/fc8861434840f9a9e65a1a6e358df2e6 to your computer and use it in GitHub Desktop.
Save smithdc1/fc8861434840f9a9e65a1a6e358df2e6 to your computer and use it in GitHub Desktop.
Benchmark for `get_parent_list()`
from django.conf.global_settings import INSTALLED_APPS
import django
from django.db import models
from django.conf import settings
import pyperf
settings.configure(INSTALLED_APPS= ('__main__',))
django.setup()
# models
# ParentListTests models
class CommonAncestor(models.Model):
pass
class FirstParent(CommonAncestor):
first_ancestor = models.OneToOneField(CommonAncestor, models.CASCADE, primary_key=True, parent_link=True)
class SecondParent(CommonAncestor):
second_ancestor = models.OneToOneField(CommonAncestor, models.CASCADE, primary_key=True, parent_link=True)
class Child(FirstParent, SecondParent):
pass
def test(loops):
range_it = range(loops)
t0 = pyperf.perf_counter()
# repeat to reduce impact of for loop
for loop in range_it:
CommonAncestor._meta.get_parent_list()
FirstParent._meta.get_parent_list()
SecondParent._meta.get_parent_list()
Child._meta.get_parent_list()
CommonAncestor._meta.get_parent_list()
FirstParent._meta.get_parent_list()
SecondParent._meta.get_parent_list()
Child._meta.get_parent_list()
CommonAncestor._meta.get_parent_list()
FirstParent._meta.get_parent_list()
SecondParent._meta.get_parent_list()
Child._meta.get_parent_list()
CommonAncestor._meta.get_parent_list()
FirstParent._meta.get_parent_list()
SecondParent._meta.get_parent_list()
Child._meta.get_parent_list()
CommonAncestor._meta.get_parent_list()
FirstParent._meta.get_parent_list()
SecondParent._meta.get_parent_list()
Child._meta.get_parent_list()
return pyperf.perf_counter() - t0
runner = pyperf.Runner()
runner.bench_time_func("get_parent_list", test)
@smithdc1
Copy link
Author

Before

.....................
get_parent_list: Mean +- std dev: 275 us +- 5 us

After

.....................
get_parent_list: Mean +- std dev: 119 us +- 3 us

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