首页 \ 问答 \ Rails 4:为什么一种更快速渲染局部的方法呢?(Rails 4: why is one way of rendering partials so much faster?)

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
更新时间:2023-09-19 14:09

最满意答案

刮擦的经验法则是不要这样做。 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 ...
  • 尝试: 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模板( ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)