→ Neisseria: 可以用瀏覽器的 developer tools 看那段程式最花時間 02/26 15:07
推 Falldog: 用django debug toolbar看看吧 說不定是query太多次 02/26 16:22
推 stupidgod08: 建議分頁或cache或配合api用javascript生成 02/26 16:55
→ uranusjr: Django Template 的 for loop 確實有可能有效能問題 02/26 18:13
→ uranusjr: 可以改用 simple tag 直接用 Python 產出 table content 02/26 18:13
推 icycandle: ajax & js render +1 只是這樣就不好用 debugtoolbar 02/28 16:36
更新:
依 uranusjr 大的建議,我用 simple tag 把 loop 拉回 python跑
在 html 中把 for loop 改成:
<table>
{% autoescape off %} {% populate_table results%}{% endautoescape %}
</table>
然後 register.simple_tag 的內容就類似 uranusjr 大提供的範例。
但測試之後發現結果還是不是很令人滿意,
len(list) simple tag | for loop in html
--------------|------------|-------------------
15 | < 4.5s | < 2.5s
--------------|------------|-------------------
150 | < 4.5s | < 2.5s
--------------|------------|-------------------
2,500 | < 5s | < 4.5s
--------------|------------|-------------------
260,000 | < 1.2 min | about 2.8 min
--------------|------------|-------------------
也就是說在有超級長的 list 時,simple tag 效能確實好很多,
但是就算 list 內容很少的時候,還是必須花到四秒鐘以上,
完全比不上原本的方法的效能,這實在讓我很困擾@@
不知道各位還有沒有什麼其它建議?
謝謝
※ 編輯: gbllggi (108.58.165.58), 03/09/2016 05:18:43
→ uranusjr: 因為解析 simple tag 需要 load external register, 會 03/12 14:50
→ uranusjr: 對 startup time 有影響, 如果仍然不能接受就只能拉到 03/12 14:50
→ uranusjr: view 裡面做, 或者改用 Jinja2 來處理這個 template 了 03/12 14:50