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?
我编写了一个Ruby应用程序,它从不同格式的源,html,xml和csv文件中解析大量数据。如何找出代码中哪些区域占用时间最长?
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?
有没有关于如何提高Ruby应用程序性能的好资源?或者您是否遵循始终遵循的任何性能编码标准?
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"
5 个解决方案
#1
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
好吧,有一些众所周知的做法,如字符串连接比“#{value}”慢,但为了找出脚本消耗大部分时间或超过要求的时间,你需要进行性能分析。有一种名为ruby-prof的红宝石。探查器甚至可以引起您可能很少发生的性能问题。我一直在使用它,发现它非常有用。以下是其官方网站上的一些信息
ruby-prof is a fast code profiler for Ruby. Its features include:
ruby-prof是Ruby的快速代码分析器。其功能包括:
Speed - it is a C extension and therefore many times faster than the standard Ruby profiler.
速度 - 它是C扩展,因此比标准Ruby分析器快许多倍。
Modes - Ruby prof can measure a number of different parameters, including call times, memory usage and object allocations.
模式 - Ruby prof可以测量许多不同的参数,包括调用时间,内存使用和对象分配。
Reports - can generate text and cross-referenced html reports
报告 - 可以生成文本和交叉引用的html报告
Flat Profiles - similar to the reports generated by the standard Ruby profiler
Flat Profiles - 类似于标准Ruby Profiler生成的报告
Graph profiles - similar to GProf, these show how long a method runs, which methods call it and which methods it calls.
图形配置文件 - 类似于GProf,它们显示了一个方法运行的时间,调用它的方法以及它调用的方法。
Call tree profiles - outputs results in the calltree format suitable for the KCacheGrind profiling tool.
调用树配置文件 - 以适合KCacheGrind配置文件工具的calltree格式输出结果。
Threads - supports profiling multiple threads simultaneously
线程 - 支持同时分析多个线程
Recursive calls - supports profiling recursive method calls
递归调用 - 支持分析递归方法调用
#2
You can test the performance of individual sections of code with the standard Benchmark module.
您可以使用标准Benchmark模块测试各个代码段的性能。
You could also test your code on different implementations of Ruby (eg 1.9, Rubinius) and see if that speeds things up.
您还可以在Ruby的不同实现上测试您的代码(例如1.9,Rubinius),看看是否加快了速度。
Of course if your problems are algorithmic in nature then there's not too much point in worrying about things like string concatenation speeds...
当然如果你的问题本质上是算法的,那么担心字符串连接速度等问题并没有太多的意义......
#3
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
字符串连接的答案如下:https://web.archive.org/web/20090122123342/http://blog.cbciweb.com/2008/06/10/ruby-performance-use-double-quotes-vs - 单引号
#4
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.
除了上面的内容,我还建议观看Scaling Ruby截屏视频。它有一些关于如何编写更快的Ruby代码的有趣提示和技巧。
#5
There is also a set of dTrace tools for Ruby you can use in the dTraceToolkit
您还可以在dTraceToolkit中使用一组用于Ruby的dTrace工具
#1
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
好吧,有一些众所周知的做法,如字符串连接比“#{value}”慢,但为了找出脚本消耗大部分时间或超过要求的时间,你需要进行性能分析。有一种名为ruby-prof的红宝石。探查器甚至可以引起您可能很少发生的性能问题。我一直在使用它,发现它非常有用。以下是其官方网站上的一些信息
ruby-prof is a fast code profiler for Ruby. Its features include:
ruby-prof是Ruby的快速代码分析器。其功能包括:
Speed - it is a C extension and therefore many times faster than the standard Ruby profiler.
速度 - 它是C扩展,因此比标准Ruby分析器快许多倍。
Modes - Ruby prof can measure a number of different parameters, including call times, memory usage and object allocations.
模式 - Ruby prof可以测量许多不同的参数,包括调用时间,内存使用和对象分配。
Reports - can generate text and cross-referenced html reports
报告 - 可以生成文本和交叉引用的html报告
Flat Profiles - similar to the reports generated by the standard Ruby profiler
Flat Profiles - 类似于标准Ruby Profiler生成的报告
Graph profiles - similar to GProf, these show how long a method runs, which methods call it and which methods it calls.
图形配置文件 - 类似于GProf,它们显示了一个方法运行的时间,调用它的方法以及它调用的方法。
Call tree profiles - outputs results in the calltree format suitable for the KCacheGrind profiling tool.
调用树配置文件 - 以适合KCacheGrind配置文件工具的calltree格式输出结果。
Threads - supports profiling multiple threads simultaneously
线程 - 支持同时分析多个线程
Recursive calls - supports profiling recursive method calls
递归调用 - 支持分析递归方法调用
#2
You can test the performance of individual sections of code with the standard Benchmark module.
您可以使用标准Benchmark模块测试各个代码段的性能。
You could also test your code on different implementations of Ruby (eg 1.9, Rubinius) and see if that speeds things up.
您还可以在Ruby的不同实现上测试您的代码(例如1.9,Rubinius),看看是否加快了速度。
Of course if your problems are algorithmic in nature then there's not too much point in worrying about things like string concatenation speeds...
当然如果你的问题本质上是算法的,那么担心字符串连接速度等问题并没有太多的意义......
#3
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
字符串连接的答案如下:https://web.archive.org/web/20090122123342/http://blog.cbciweb.com/2008/06/10/ruby-performance-use-double-quotes-vs - 单引号
#4
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.
除了上面的内容,我还建议观看Scaling Ruby截屏视频。它有一些关于如何编写更快的Ruby代码的有趣提示和技巧。
#5
There is also a set of dTrace tools for Ruby you can use in the dTraceToolkit
您还可以在dTraceToolkit中使用一组用于Ruby的dTrace工具