CSC/ECE 517 Fall 2013/ch1 1w02 pp: Difference between revisions
No edit summary |
|||
Line 23: | Line 23: | ||
There are many Profiling tools available for analyzing the ruby programs. Ruby itself has inbuilt profilers in the form of modules. Two such modules are Profiler__ and Benchmark module. We will look how to use these tools to profile our ruby code. | There are many Profiling tools available for analyzing the ruby programs. Ruby itself has inbuilt profilers in the form of modules. Two such modules are Profiler__ and Benchmark module. We will look how to use these tools to profile our ruby code. | ||
=== | ===Profiler=== | ||
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. This profile.rb source file has the program that measures the performance of the system by recording the function calls. Here, the input is a collection of all the function calls made in the code. Specifically, profile.rb uses the method ‘ kernel#set_trace_func’ to keep a track of all function calls. | This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. This profile.rb source file has the program that measures the performance of the system by recording the function calls. Here, the input is a collection of all the function calls made in the code. Specifically, profile.rb uses the method ‘ kernel#set_trace_func’ to keep a track of all function calls. | ||
How to use profile.rb to profile our code: | |||
1) Create a ruby file profiler_example.rb | |||
{{ | |||
#profile_example.rb code begins | |||
require 'profile' | |||
def slow_method | |||
10000.times do 9999999999999999*999999999 | |||
end | |||
end | |||
slow_method | |||
#profile_example.rb code ends | |||
}} |
Revision as of 23:10, 16 September 2013
Introduction to Profiling
Profiling in general, is an important feature in computer science. It is technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the programs time complexity or space complexity, and some other related performance parameters. For Example, the time complexity includes total running time, CPU time, memory used, time taken by each module or function, function calls, response time and many similar important aspects of our program/application. Profiling takes up the significant steps or constructs (like loop statements, statements involving operations and functions like aggregate functions, individual blocks and modules) in the program source code for performance study.
Ruby profiling is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution give values for a set of parameters that define the performance of the input ruby program. There are many types of profiling method like event-based and statistical methods. Ruby mostly uses event-based profilers. Here in case of Ruby profiler, we get the analysis results in various formats like table, graphs etc.
As we can see the flow we give the ruby program as input to the Ruby profiling tools and that's it we will get a profiler output, which will help us in optimizing out code.
Significance of Profiling
The main purpose of profiling is:
- To analyze the performance of the code, their CPU utilization, memory operations and also to find out bottlenecks in our program.
- Based on the results presented by the profiler, one can improve upon the code with better constructs and functions and hence we can easily optimize the code in accord with the system requirements.
- We can accurately measure how a program functions or performs in one given environment and with different input data.
- To identify the bottlenecks in a program, the portions that actually cause program overhead or can slow down the system o need special testing or can raise exception.
Ruby Profiling tools
There are many Profiling tools available for analyzing the ruby programs. Ruby itself has inbuilt profilers in the form of modules. Two such modules are Profiler__ and Benchmark module. We will look how to use these tools to profile our ruby code.
Profiler
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. This profile.rb source file has the program that measures the performance of the system by recording the function calls. Here, the input is a collection of all the function calls made in the code. Specifically, profile.rb uses the method ‘ kernel#set_trace_func’ to keep a track of all function calls.
How to use profile.rb to profile our code: 1) Create a ruby file profiler_example.rb
{{
- profile_example.rb code begins
require 'profile'
def slow_method 10000.times do 9999999999999999*999999999 end end slow_method
- profile_example.rb code ends
}}