【发布时间】:2013-11-22 06:06:29
【问题描述】:
嗨, 我有一个有点复杂的字典要渲染。 现在渲染它大约需要 10-15 秒,这显然是不可取的。
目前我有一个包含 237 个键的字典(可能少至三个,多至 700 个左右)。
SQL 是手动完成的(cur.execute 和 cur.fetchall()),因此模板本身不应该发生任何惰性(调试工具栏也同意这一点)。 分析表明它是纯渲染,即使我没有在模板中的任何地方绘制这个大字典,它也很慢。 如果我完全省略将它传递给 render() 函数,那么一切都应该是那么敏捷。
我到底应该怎么做才能加快速度?
一个元素为 200+ 的字典看起来有点像这样:
data[entry_id] ={
{
'status': 200,
'statustext': u'OK',
'rhttpversion': u'HTTP',
'host': u'l.linkpulse.com',
'entry_id': 16484,
'port': None,
'uri': u'http: //l.linkpulse.com/p.gif',
'response_header': [
{
'name': u'Connection',
'value': u'keep-alive'
},
{
'name': u'Content-Length',
'value': u'43'
},
{
'name': u'Server',
'value': u'Apache/2.2.14(Ubuntu)'
},
{
'name': u'Last-Modified',
'value': u'Mon,
07May200711: 49: 44GMT'
},
{
'name': u'ETag',
'value': u'"8607e-2b-42fe5a7"'
},
{
'name': u'Date',
'value': u'Sat,
09Nov201317: 34: 40GMT'
},
{
'name': u'Content-Type',
'value': u'image/gif'
},
{
'name': u'Accept-Ranges',
'value': u'bytes'
}
],
'request_header': [
{
'name': u'Referer',
'value': u'http://www.xxx.com/'
},
{
'name': u'Accept-Language',
'value': u'en-US,
en;q=0.5'
},
{
'name': u'Accept',
'value': u'text/html,
application/xhtml+xml,
application/xml;q=0.9,
*/*;q=0.8'
},
{
'name': u'Host',
'value': u'l.linkpulse.com'
},
{
'name': u'User-Agent',
'value': u'Mozilla/5.0(Windows;U;WindowsNT6.1;en-US;rv: 24.0)Gecko/20100101Firefox/24.0'
},
{
'name': u'Accept-Encoding',
'value': u''
}
],
'method': u'GET',
'httpversion': u'HTTP'
}
}
编辑: sys.sizeof(data) 为 280。 请注意,麻烦的 dict 中的每个元素都有两个数组,平均有 6-7 个 dicts(每个有两个键/值对)。
模板看起来像这样:
{% extends "layouts/full.html" %}
{% block content %}
...
{% include 'report/headers.html' %}
...
报告/headers.html:
<table class="table table-striped table-bordered table-hover headers">
<thead>
<th class="hheader" style="text-align:center">Request</th>
<th class="hheader" style="text-align:center">Response</th>
</thead>
<tbody>
{% for key,header in headers.items %}
<tr class="connection">
<td class="headers">
<b>
{{ header.method }} {{ header.uri }} {{ header.rhttpversion }}
<br>
<br>
</b>
<div class="headers">
{% for request in header.request_header %}
<b> {{ request.name }}: </b> {{ request.value }} <br>
{% endfor %}
</div>
</td>
<td class="headers">
<b>
{{ header.status }} {{ header.statustext }} {{ header.httpversion }}
<br>
<br>
</b>
<div class="headers">
{% for response in header.response_header %}
<b> {{ response.name }}: </b> {{ response.value }} <br>
{% endfor %}
</div>
{% endfor %}
</td>
</tr>
</tbody>
</table>
分析中的相关部分: 模板计时:https://www.dropbox.com/s/rlqym0akg45sur1/Screenshot%202013-11-10%2019.03.36.png 时间:https://www.dropbox.com/s/lkuwfsb8sygi1es/Screenshot%202013-11-10%2019.20.49.png
我很快就会用“个人资料”选项卡中的信息进行更新。由于某种原因,它现在是灰色的。
可以在此处查看包含数据的站点:http://two.mrfjo.org/1316c6f4-72c1-46b6-818c-31121d2c9712/(忽略 snort 警报,它们不是真实的)。
【问题讨论】:
-
如何渲染它?它可能会对渲染性能产生影响。
-
将其渲染为“正常”?大大地。 return render(request, 'report.html', data ) 其中 data 是我的字典。
-
我的意思是用来渲染字典的模板。
-
您可以添加个人资料报告的相关部分吗?什么返回
sys.getsizeof(data)? -
添加了更多信息和调试
标签: python django performance dictionary django-templates