首页 \ 问答 \ 佣工真的比分数更快吗?(Are helpers really faster than partials? What about string building?)

佣工真的比分数更快吗?(Are helpers really faster than partials? What about string building?)

我在Rails应用程序中加入了一个花哨的“工作表”样式视图,该应用程序的加载时间太长。 (在开发模式下,是的,我知道那里没有缓存,“在57893ms完成(查看:54975,DB:855)”)工作表是使用帮助器方法呈现的,因为我无法忍受维护很多小部分工作表中不同种类的行。 现在我想知道partials是否可能更快?

我分析了页面加载情况,并确定了一些情况,其中对象缓存将削减几秒钟,但配置文件输出表明大量时间花在循环浏览Worksheet模型的组成对象并将字符串输出附加到帮手。 以下是我正在谈论的一个例子:

def header_row(wksht)
  content_tag(:thead, :class => "ioe") do
    content_tag(:tr) do
      html_row = []
      for i in (0...wksht.class::NUM_COLS) do
        html_row << content_tag(:th, h(wksht.column_headings[i].upcase),
                                :class => wksht.column_classes[i])
      end
      html_row.join("\n")
    end
  end
end

使用partials的OTOH意味着打开文件,脱离Ruby解释器,并且从长远来看,聚合一串字符串,对吧? 所以我想知道是否有另一种方法来加速助手的工作。 我是否应该使用类似stringstream的东西(它存在于Ruby中吗?),我应该去掉content_tag调用,以支持自己的字符串插值......我愿意编写自己的性能测试,并共享结果,如果你有任何建议的替代方法,我已经采取了。

由于这是一个相当复杂的视图(并且有一个可编辑的版本),所以我宁愿不要重写和重复整个过程。 :)

一些相关的阅读:

http://www.viget.com/extend/helpers-vs-partials-a-performance-question/ (旧)
http://www.breakingpointsystems.com/community/blog/ruby-string-processing-overhead/
http://blog.purepistos.net/index.php/2008/07/14/benchmarking-ruby-string-interpolation-concatenation-and-appending/

@tadman:有行总计和列总计(和更多的列算术),并且因为它们不仅仅是总计,而且还取决于数据库中的其他“幻数”,所以我在Ruby代码而不是Javascript中实现了它们。 (DRY和单元测试。)Javascript仅用于编辑视图,仅用于添加/删除行(仅用于客户端),并在单元格内容更改时用新总计获取表单。 它获取整个表格,因为当输入单元格更改时,将会更新近一半的值。

工作表及其行实际上是虚拟模型; 他们不住在数据库中,而是聚集一堆真正的AR对象。 每次视图呈现时都会创建它们(但在开发模式下需要1.7秒,所以我不担心它)。

我想我可以传送一个数字矩阵,而不是标记的内容,然后让JS将它解压缩到表格中。 但是这个速度难以维持。


I've got a fancy-schmancy "worksheet" style view in a Rails app that is taking way too long to load. (In dev mode, and yes I know there's no caching there, "Completed in 57893ms (View: 54975, DB: 855)") The worksheet is rendered using helper methods, because I couldn't stand maintaining umpteen teeny little partials for the different sorts of rows in the worksheet. Now I'm wondering whether partials might actually be faster?

I've profiled the page load and identified a few cases where object caching will shave a few seconds off, but the profile output suggests that a large chunk of time is spent simply looping through the Worksheet model's constituent objects and appending the string output from the helper. Here's an example of what I'm talking about:

def header_row(wksht)
  content_tag(:thead, :class => "ioe") do
    content_tag(:tr) do
      html_row = []
      for i in (0...wksht.class::NUM_COLS) do
        html_row << content_tag(:th, h(wksht.column_headings[i].upcase),
                                :class => wksht.column_classes[i])
      end
      html_row.join("\n")
    end
  end
end

OTOH using partials means opening files, spinning off the Ruby interpreter, and in the long run, aggregating a bunch of strings, right? So I'm wondering whether there is another way to speed things up in the helpers. Should I be using something like a stringstream (does that exist in Ruby?), should I get rid of content_tag calls in favor of my own "" string interpolation... I'm willing to write my own performance tests, and share the results, if you have any suggested alternatives to the approach I've already taken.

As it's a fairly complex view (and has an editable version as well), I'd rather not rewrite-and-profile the whole thing more than once. :)

Some related reading:

http://www.viget.com/extend/helpers-vs-partials-a-performance-question/ (old)
http://www.breakingpointsystems.com/community/blog/ruby-string-processing-overhead/
http://blog.purepistos.net/index.php/2008/07/14/benchmarking-ruby-string-interpolation-concatenation-and-appending/

@tadman: There are row totals and column totals (and more columnar arithmetic), and since they're not all just totals, but also depend on other "magic numbers" from the database, I implemented them in the Ruby code rather than Javascript. (DRY and unit testable.) Javascript is used only in the edit view, and just to add/delete rows (client side only) and to fetch a sheet with fresh totals when the cell contents change. It fetches the whole table because nearly half of the values get updated when an input cell changes.

The worksheet and its rows are actually virtual models; they don't live in the DB, but rather aggregate a boatload of real AR objects. They get created every time a view renders (but that takes 1.7 secs in dev mode, so I'm not worried about it).

I suppose I could transmit a matrix of numbers, rather than marked-up content, and have JS unpack it into the sheet. But that gets unmaintainable fast.


原文:https://stackoverflow.com/questions/3426273
更新时间:2023-07-10 18:07

最满意答案

您可以通过file_get_contents获取http://www.example.com/template.php的内容

$result = $client->sendEmail([
    'Message' => [
        'Body' => [
            'Html' => [
                'Data' => file_get_contents('http://www.example.com/template.php'),
            ],
            'Text' => [
                'Data' => '<string>',
            ],
        ]
    ]
]);

You can get contents of http://www.example.com/template.php by file_get_contents

$result = $client->sendEmail([
    'Message' => [
        'Body' => [
            'Html' => [
                'Data' => file_get_contents('http://www.example.com/template.php'),
            ],
            'Text' => [
                'Data' => '<string>',
            ],
        ]
    ]
]);

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用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)