I think it's a good habit to take a look at the profile as well as write the test when implementing a bunch of procedures. Optimizing too early isn't good, but just recognizing a small bottleneck before a performance problem becomes apparent gives you the leeway to "tune at any time."
# coding:utf-8
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
import django
django.setup()
if __name__ == "__main__":
"""
$ cd /path/to/project
$ python -m project.profile
"""
import cProfile
import pstats
#The function you want to see in the profile
from app.models import func_to_profile
prf = cProfile.Profile()
prf.runcall(func_to_profile)
pstats.Stats(prf
).sort_stats('time' #Internal time sort in descending order
).print_stats(.03) #Top 3%Show only
The result display looks like this
$ python -m project.profile
9109096 function calls (8991335 primitive calls) in 8.224 seconds
Ordered by: internal time
List reduced from 463 to 14 due to restriction <0.03>
ncalls tottime percall cumtime percall filename:lineno(function)
125620 0.723 0.000 0.723 0.000 /path/to/django/utils/dateparse.py:107(<dictcomp>)
62810 0.631 0.000 0.805 0.000 /path/to/django/db/models/base.py:388(__init__)
125620 0.435 0.000 1.702 0.000 /path/to/django/utils/dateparse.py:84(parse_datetime)
265490 0.395 0.000 0.640 0.000 /path/to/django/db/models/fields/related.py:1619(local_related_fields)
670 0.339 0.001 3.027 0.005 {method 'fetchmany' of 'sqlite3.Cursor' objects}
125620 0.292 0.000 2.242 0.000 /path/to/django/db/backends/sqlite3/utils.py:6(parse_datetime_with_timezone_support)
279460 0.291 0.000 0.474 0.000 /path/to/django/db/models/fields/related.py:1633(get_instance_value_for_fields)
265490 0.205 0.000 1.209 0.000 /path/to/django/db/models/fields/related.py:1627(get_local_related_value)
549522 0.199 0.000 0.199 0.000 {hasattr}
127330 0.198 0.000 0.198 0.000 {method 'match' of '_sre.SRE_Pattern' objects}
672800/561660 0.194 0.000 0.423 0.000 {setattr}
127330 0.175 0.000 0.175 0.000 {method 'groupdict' of '_sre.SRE_Match' objects}
55570 0.157 0.000 0.263 0.000 /path/to/django/db/models/fields/related.py:615(__set__)
376960 0.154 0.000 0.254 0.000 /path/to/django/db/models/fields/related.py:1609(related_fields)(app)
Looking at the result, it seems that it is difficult to parse sqlite data to datetime
.
You can see that it is possible to take measures such as narrowing down the columns to be parsed in case of emergency.
Recommended Posts