Rails 4:为什么一种更快速渲染局部的方法呢?(Rails 4: why is one way of rendering partials so much faster?)
我不确定这是否是Rails特定问题,因此我也将其标记为
ruby
。我通过部分渲染
event
记录的集合。 但是,我发现不同地呈现部分会导致严重的性能差异。两个版本都使用完全相同的数据,唯一改变的是用于渲染部分的代码。
为什么heck是一个版本始终比另一个版本快4倍? 让我想知道我正在采取什么样的性能......
慢版(950ms总请求时间):
<% events.each do |event| %> <%= render partial: "events/event", locals: { event: event } %> <% end %> # Log output Rendered events/_event.html.erb (1.1ms) Rendered events/_event.html.erb (1.1ms) ...
更快的版本(总请求时间250ms):
<%= render partial: "events/event", collection: events, as: :event %> # Log output Rendered events/_event.html.erb (58.7ms)
I'm not sure if this is a Rails specific issue, hence the reason I tagged it
ruby
as well.I'm rendering a collection of
event
records via a partial. However, I found that rendering the partial differently results in drastic performance differences.Both version use the exact same data, the only thing changing is the code used to render the partials.
Why the heck is one version consistently 4x faster than the other? Makes me wonder what other performance hits I'm taking...
Slow version (950ms total request time):
<% events.each do |event| %> <%= render partial: "events/event", locals: { event: event } %> <% end %> # Log output Rendered events/_event.html.erb (1.1ms) Rendered events/_event.html.erb (1.1ms) ...
Faster version (250ms total request time):
<%= render partial: "events/event", collection: events, as: :event %> # Log output Rendered events/_event.html.erb (58.7ms)
原文:https://stackoverflow.com/questions/44509928
最满意答案
刮擦的经验法则是不要这样做。 API中提供了许多内容(使用API沙箱来发现它们)。 对于大多数其他有趣的数据,有人已经写了一个库
在这种情况下, action = query&titles = The_Sanctuary&prop = coordinates将为您提供所需内容:
{ "batchcomplete": "", "query": { "pages": { "788970": { "pageid": 788970, "ns": 0, "title": "The Sanctuary", "coordinates": [ { "lat": 51.41, "lon": -1.83173, "primary": "", "globe": "earth" } ] } } } }
Rule of thumb for scraping is to not do it. Many things are available in the API (use the API sandbox to discover them). For most other interesting data someone already wrote a library.
In this case, action=query&titles=The_Sanctuary&prop=coordinates will get you what you want:
{ "batchcomplete": "", "query": { "pages": { "788970": { "pageid": 788970, "ns": 0, "title": "The Sanctuary", "coordinates": [ { "lat": 51.41, "lon": -1.83173, "primary": "", "globe": "earth" } ] } } } }
相关问答
更多-
而不是使用单个for循环。 你可以分成两个循环,外循环获取页面,内循环你可以进入修订。 for pageid, pagedetails in res["query"]["pages"].iteritems(): for revision in pagedetails["revisions"]: if revision["user"] == "TechBot": do.something() Instead of using a single for loop ...
-
查询维基百科数据页面(Query Wikipedia data page)[2023-04-20]
尝试: import urllib import urllib2 import simplejson url = 'http://en.wikipedia.org/w/api.php' values = {'action' : 'query', 'prop' : 'revisions', 'titles' : 'Jennifer_Aniston', 'rvprop' : 'content', 'format' : 'json' ... -
我不确定为什么你会困惑(如果你解释了这一点,这将有所帮助),但我确信查询不是你想要的。 它在页面“标题”( titles=Title )中链接( prop=links )链接( prop=links )链接( generator=links )。 它还只列出链接第一页上的第一页链接(页面大小为微小的默认值10)。 如果你想获得页面上“标题”的所有链接: 只需使用prop=links ,你不需要生成器。 通过添加pllimit=max ( pl是links的“前缀”)将限制增加到最大可能pllimit=max ...
-
只需使用页面作为数组: $response = json_decode(file_get_contents('https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=Stack%20Overflow')); $pages = (array) $response ->query->pages; foreach ($pages as $id => $page) { echo $pag ...
-
维基百科没有一个类别比其他类别更主要的概念,并且排序也没有帮助(它反映了源中的顺序,这通常意味着自动生成的类别,最后的重要类别)。 您最好的选择可能是使用Wikidata API并获取“instance of”属性的值。 例如, HPatDHp2是电影的一个实例。 Wikipedia has no concept of one category being more main than the others, and the ordering does not help either (it reflect ...
-
提交表单时,它实际上应用了浏览器定义的默认行为,这意味着它会将您重定向到表单的action属性。 实际上,因为action设置为# ,所以页面只是刷新。 要解决此问题,您应该通过简单地使用以下内容来阻止默认行为: $(document).ready(function() { $("#form").submit(function(e) { e.preventDefault(); 将事件对象作为参数添加到提交事件处理程序( e ),并对其应用preventDefault方法。 看看这个 ...
-
刮擦的经验法则是不要这样做。 API中提供了许多内容(使用API沙箱来发现它们)。 对于大多数其他有趣的数据,有人已经写了一个库 在这种情况下, action = query&titles = The_Sanctuary&prop = coordinates将为您提供所需内容: { "batchcomplete": "", "query": { "pages": { "788970": { "pageid": 78897 ...
-
英语维基百科中关于视频游戏的所有页面都包含名为Infobox视频游戏的模板,因此您只需要使用维基百科API查询属性transcludedin来获取所有这些: https://en.wikipedia.org/w/api.php?action=query&prop=transcludedin&tilimit=500&titles=Template:Infobox_video_game All pages in English Wikipedia about video games contain templ ...
-
您可以通过两个步骤使用MediaWiki API和action = parse来获取每个页面部分的内容。 首先,您必须从页面中获取所有部分: https://en.wikipedia.org/w/api.php?action=parse&prop=sections&page=Wikipedia:Unusual_articles 从响应中您可以看到Americas 索引= T-2 ( T表示被转换的页面),它来自fromtitle = Wikipedia:Unusual_articles/Places_an ...
-
这不是一个空白的部分,它是包含2014 FIFA World Cup Goalscorers模板的部分。 你需要得到这个模板的内容: http : //en.wikipedia.org/w/api.php? action = query&prop = latest&title =模板: 2014% 20FIFA%20World%20Cup%20Goalscorers&rvprop = content但是哦shi ~~,它是重定向到另一个名为2014 FIFA World Cup goalscorers模板( ...