Created
January 15, 2021 13:15
-
-
Save smithdc1/fc8861434840f9a9e65a1a6e358df2e6 to your computer and use it in GitHub Desktop.
Benchmark for `get_parent_list()`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Before
After