<p>Hello, </p>
<p>go newbie here. So I am reading the posts below and I'm super surprised by it. A year later, is the use of reflection that causes slowness still a thing in latest version?</p>
<p><a href="http://stackoverflow.com/questions/31361745/slow-performance-of-html-template-in-go-lang-any-workaround" rel="nofollow">http://stackoverflow.com/questions/31361745/slow-performance-of-html-template-in-go-lang-any-workaround</a></p>
<p><a href="https://www.reddit.com/r/golang/comments/3d6j42/why_is_htmltemplate_so_slow/" rel="nofollow">https://www.reddit.com/r/golang/comments/3d6j42/why_is_htmltemplate_so_slow/</a></p>
<hr/>**评论:**<br/><br/>Sphax: <pre><p>Just use html/template and worry about its performance when it's becoming a problem. </p></pre>MrCowPie: <pre><p>i can't agree more. This post is more for my curiosity than anything else. </p></pre>Sphax: <pre><p>well html/template still uses reflection and as far as I know there hasn't been gigantic speed improvements in the reflect package. in one year we got global speed improvements though with go 1.6 and 1.5 so there's that. </p>
<p>unfortunately I don't have experience at high volume using html/template so I can't give you figures. I do use encoding/json which is reflection heavy on a bunch of micro services with a bit of traffic and from the profiling I've done it's rarely the bottleneck. </p></pre>: <pre><p>[deleted]</p></pre>valyala: <pre><p>Actually, there are much faster dynamic template engines without Go gen exist - see <a href="https://github.com/CloudyKit/jet" rel="nofollow">jet</a> as an example.</p></pre>GoTheFuckToBed: <pre><p>just do it</p></pre>MrCowPie: <pre><p>researching is part of doing. </p></pre>xrstf: <pre><blockquote>
<p>"One good test is worth a thousand expert opinions."</p>
<p>(Wernher von Braun)</p>
</blockquote></pre>MrCowPie: <pre><p>noted and tested. with a database connection and simple select statement, go is about 3-4x faster than php</p></pre>runicnet: <pre><p>sounds like procrastination, </p></pre>tscs37: <pre><p>the template package still uses a lot of reflection, not as fast as it could be.</p>
<p>The best method would be to setup a database with expected latencies and test if the time it takes the page to render actually impacts the performance significantly, chances are the DB connection will be slower.</p>
<p>Secondly, you could design the template render as a standalone static server to serve these templates, thusly you can more easily scale them upwards by just adding more of them (aka microservices)</p>
<p>Lastly, you could try to design a SPA instead, that requires to render a template at worst once instead of many pages, the backend then uses an API to communicate.</p></pre>MrCowPie: <pre><p>well I don't know if the html template is as slow as said in the link posted above but I took your advice and tested with a database connection. The code will print out a resultset of about 20 strings to the html page. For comparison, I wrote a similar code using php7 with opcode caching enabled. Unless I'm doing something wrong, golang is about 3-4x faster in my test.</p></pre>tscs37: <pre><p>That sounds about reasonable.</p>
<p>The PHP7 code is fully interpreted while Go only needs to interprete and reflect on the template code, which should slow it down a bit but the rest gives it sufficient boost over PHP.</p></pre>drvd: <pre><p>Slow compared to what?</p>
<p>"Is 2m short?" is a useless question: 2m ist huge for amoeba and tiny for stars. </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传