Far*_*abi 5 python django execution-time django-views python-3.x
这是我的观点:
def post_detail(request, year, month, day, slug):
post = get_object_or_404(models.Post, slug=slug, status='published',
publish__year=year, publish__month=month,
publish__day=day)
comment_form = forms.CommentForm()
comments = post.comments.filter(active=True)
context = {
'comments': comments,
'post': post,
'comment_form': comment_form,
}
return render(request, 'blog/post_detail.html', context)
Run Code Online (Sandbox Code Playgroud)
有什么方法可以计算 Django 中的执行时间吗?
min*_*lyu 10
您可以编写一个计时器装饰器来在控制台中输出结果
from functools import wraps
import time
def timer(func):
"""helper function to estimate view execution time"""
@wraps(func) # used for copying func metadata
def wrapper(*args, **kwargs):
# record start time
start = time.time()
# func execution
result = func(*args, **kwargs)
duration = (time.time() - start) * 1000
# output execution time to console
print('view {} takes {:.2f} ms'.format(
func.__name__,
duration
))
return result
return wrapper
@timer
def your_view(request):
pass
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5805 次 |
| 最近记录: |