如何找到 Ruby 应用程序中的性能瓶颈?
我编写了一个 Ruby 应用程序,它可以解析来自不同格式 html、xml 和 csv 文件的源的大量数据。 如何找出代码的哪些区域花费时间最长?
有没有关于如何提高 Ruby 应用程序性能的好资源? 或者您是否有始终遵循的性能编码标准?
例如,您是否总是将字符串与
output = String.new
output << part_one
output << part_two
output << '\n'
或将使用
output = "#{part_one}#{part_two}\n"
I have written a Ruby application which parses lots of data from sources in different formats html, xml and csv files. How can I find out what areas of the code are taking the longest?
Is there any good resources on how to improve the performance of Ruby applications? Or do you have any performance coding standards you always follow?
For example do you always join your string with
output = String.new
output << part_one
output << part_two
output << '\n'
or would you use
output = "#{part_one}#{part_two}\n"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,有一些众所周知的做法,例如字符串连接比“#{value}”慢得多,但为了找出脚本在哪里消耗了大部分时间或比所需时间更多的时间,您需要进行分析。 有一种红宝石叫做 ruby-prof。 即使是那些很少发生的性能问题,分析器也可以引起您的注意。 我已经经常使用它并且发现它非常有帮助。 以下是直接来自其官方网站的一些信息
Well, there are certain well known practices like string concatenation is way slower than "#{value}" but in order to find out where you script is consuming most of its time or more time than required, you need to do profiling. There is a ruby gem called ruby-prof. The profiler can bring to your notice even those performance issues that may rarely occur. I have been using it a lot and find it very helpful. Here is some information about it directly from its official site
您可以使用标准 基准模块。
您还可以在 Ruby 的不同实现(例如 1.9、Rubinius)上测试您的代码,看看是否可以加快速度。
当然,如果您的问题本质上是算法问题,那么担心字符串连接速度等问题就没有太多意义......
You can test the performance of individual sections of code with the standard Benchmark module.
You could also test your code on different implementations of Ruby (eg 1.9, Rubinius) and see if that speeds things up.
Of course if your problems are algorithmic in nature then there's not too much point in worrying about things like string concatenation speeds...
字符串连接的答案在这里: https://web.archive.org/web/20090122123342/http://blog.cbciweb.com/2008/06/10/ruby-performance -使用双引号与单引号
The answer to the string concatenation is here: https://web.archive.org/web/20090122123342/http://blog.cbciweb.com/2008/06/10/ruby-performance-use-double-quotes-vs-single-quotes
除了上面写的内容之外,我还建议观看 Scaling Ruby 截屏视频。 它提供了一些关于如何编写更快的 Ruby 代码的有趣提示和技巧。
Besides what's written above I also recommend watching the Scaling Ruby screencast. It has some interesting tips&tricks on how to write faster Ruby code.
您还可以在 dTraceToolkit 中使用一组适用于 Ruby 的 dTrace 工具
There is also a set of dTrace tools for Ruby you can use in the dTraceToolkit