Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the performance bottlenecks in my Ruby application?

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"
like image 460
Geekygecko Avatar asked Jan 26 '09 15:01

Geekygecko


3 Answers

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-prof is a fast code profiler for Ruby. Its features include:

Speed - it is a C extension and therefore many times faster than the standard Ruby profiler.

Modes - Ruby prof can measure a number of different parameters, including call times, memory usage and object allocations.

Reports - can generate text and cross-referenced html reports

Flat Profiles - similar to the reports generated by the standard Ruby profiler

Graph profiles - similar to GProf, these show how long a method runs, which methods call it and which methods it calls.

Call tree profiles - outputs results in the calltree format suitable for the KCacheGrind profiling tool.

Threads - supports profiling multiple threads simultaneously

Recursive calls - supports profiling recursive method calls

like image 98
Chirantan Avatar answered Nov 15 '22 03:11

Chirantan


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...

like image 26
user4812 Avatar answered Nov 15 '22 04:11

user4812


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

like image 42
Rafael Mueller Avatar answered Nov 15 '22 03:11

Rafael Mueller