佣工真的比分数更快吗?(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
最满意答案
您可以通过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>', ], ] ] ]);
相关问答
更多-
您可以使用parse_url()和parse_str() 。 $parts = parse_url($url); parse_str($parts['query'], $query); echo $query['email']; 如果要使用PHP动态获取$url ,请查看此问题: 在PHP中获取完整的URL You can use the parse_url() and parse_str() for that. $parts = parse_url($url); parse_str($parts['qu ...
-
Python URL获取中间字符串(Python URL get middle string)[2023-07-27]
你快到了。 由于您想要的只是数字,您可以使用\d仅捕获数字: >>> m = re.search('-(\d+).html', string).group(1) >>> print (m) 534641 另一种方法是告诉'所有人物除外- ': >>> m = re.search('-([^-]+).html', string).group(1) >>> print (m) 534641 有关详细信息,请参阅文档 。 一些快速说明: .html应该是\.html ,避免使用python使用的'string ... -
我使用strip_tags函数删除所有HTML标记,然后使用preg_match_all()来获取每个URL: $regex = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/"; preg_match_all($regex, strip_tags(html_entity_decode($desc)), $url); print_r($url); 我希望这有助于将来的其他人! 彼得 I used the strip_tags fun ...
-
从URL获取HTML字符串(Get HTML string from URL)[2023-03-03]
您可以通过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'), ], ... -
如果您的目标是知道服务器上是否存在该文件,则应该检查响应状态而不是请求是否成功。 为此,请更改您的ajax success函数,如下所示: success: function(data, textStatus, xhr) { if (xhr.status == 200) booTMP = true; } 或者更好的是,使用ajax complete功能,如下: complete: function(xhr, textStatus) { if (xhr.status == 200) booT ...
-
描述 =]|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\ssrc=['"]([^"]*)['"]?)(?:[^>=]|='[^']*'|="[^"]*"|=[^'"\s]*)*"\s?\/?> 这个正则表达式将执行以下操作: 这个正则表达式捕获整个IMG标签 将源属性值放入捕获组1中,如果它们存在,则不带引号。 允许属性具有单引号,双引号或不引号 可以修改以验证任何数量的其他属性 避免陷于困难边缘的情况,这往往会使解析HTML变得困难 ...
-
“如果没有,它会显示一条消息。” 通常在纯HTML中完成的操作是这样的: 错误消息始终存在,但如果视频实际加载则会被覆盖。 "if not it will display a message instead." What is normally done ...
-
我认为这应该适用于QUrlQuery QUrlQuery query; query.addQueryItem("yourKey", yourHtmlText); QUrl url = ....; url.setQuery(query); I think that should work with QUrlQuery QUrlQuery query; query.addQueryItem("yourKey", yourHtmlText); QUrl url = ....; url.setQuery(qu ...
-
您可以使用(http|https):\/\/来匹配http或https! You can use (http|https):\/\/ to match both http or https!
-
从HTML字符串中提取URL(Extract URL from string of HTML)[2023-07-29]
为此目的使用HTML解析器。 以下是嵌入式Java HTML解析器的示例。 还有像JSoup这样的其他选择,但对于基本的HTML处理,这个做得非常好: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; ...