Django Database Functions Performance Test
- In this post we will measure performance for django database function(Concat) and python f string concatenation for some data.
IPython
,Python 3.8.5
,Django 3.2.2
were used.
x
In [1]: from django.db.models import Value
In [2]: from django.db.models.functions import Concat
In [5]: def load_fullnames_v1(limit):
...: names = list(
...: Person.objects.annotate(
...: full_name=Concat('fname',Value(" "),'lname')
...: ).values_list('full_name')[:limit]
...: )
...: return names
...:
In [6]: def load_fullnames_v2(limit):
...: names = [f"{p.fname} {p.lname}" for p in Person.objects.all()[:limit]]
...: return names
...:
In [7]: %timeit for i in range(100): load_fullnames_v1(100)
83.5 ms ± 856 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [8]: %timeit for i in range(100): load_fullnames_v2(100)
89.6 ms ± 860 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [9]: %timeit for i in range(100): load_fullnames_v1(10000)
1.06 s ± 9.11 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [10]: %timeit for i in range(100): load_fullnames_v2(10000)
6.68 s ± 41.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [11]: %timeit for i in range(100): load_fullnames_v1(100000)
10.8 s ± 91.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [12]: %timeit for i in range(100): load_fullnames_v2(100000)
1min 7s ± 2.47 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
Limit | Django DB function | Python f string |
---|---|---|
100 | 83.5 ms | 89.6 ms |
10000 | 1.06 s | 6.68 s |
100000 | 10.8 s | 1 min 7 s |
Conclusion
- Django database functions are higher performance than
f string
or any other concatenation.
Yorumlar