我在装有 Python 2.7 的 32 位 Ubuntu 机器上使用 Django。我的开发服务器一直很慢,渲染任何页面大约需要 15 秒。我运行了 cProfile 测试以查看是什么工作如此缓慢。
好像是pprint模块。
这是我的统计数据:
ncalls tottime percall cumtime percall filename:lineno(function)
272605/48718 24.238 0 49.213 0.001 pprint.py:247(_safe_repr)
这是我同事的,他运行 64 位 OS X:
14531/5334 1.016 0.000 2.199 0.000 pprint.py:247(_safe_repr)
同时我必须关闭 Debug模式才能正常使用开发服务器。
这是分析脚本:
from cProfile import Profile
from django.test.client import Client
import Cookie
cl = Client()
cl.cookies = Cookie.SimpleCookie({'sessionid':'7344ebeba093b65c1d59a9d7583f60bc'})
p = Profile()
p.runctx("c.get('/welcome/')", globals={'c': cl}, locals={})
p.print_stats()
(sessionid cookie 用于显示您需要登录的页面。)
我不确定32位系统是主要原因。
主要问题是:为什么 pprint._safe_repr 在 Python 2.7 32 位中这么慢而在 64 位中这么快?我是否可以设置一些东西让它变快。
最佳答案
Why is pprint._safe_repr so slow in Python 2.7 32 bits and fast in 64 bits?
它在 64 位上并不快。您的同事接到的 ncall
少得多。
您应该调查为什么单个 GET 会导致对 _safe_repr()
的如此大量的调用。
关于python - Pprint 模块在 32 位系统中与 Django 配合使用缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8662050/