<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pkadam</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pkadam"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Pkadam"/>
	<updated>2026-06-13T05:45:29Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81912</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81912"/>
		<updated>2013-10-31T01:52:52Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Intrdouction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project (Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed and issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and implement it on the existing Expertiza system and test it.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The existing Expertiza system has an automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory has 18 files which provides features such as spell checker, plagiarism check and the review tone analysis.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We have converted automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system and include all those features in the Gem.Thus we can use the gem to include its functionality like review analysis etc.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The Automated metareview functionality is not used in the existing Expertiza system. If this feature is converted into a gem, then it can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Steps to create Gem===&lt;br /&gt;
&lt;br /&gt;
We have turned the automated_metareview folder into a gem called “automated_metareview” which could be used in other review systems.&lt;br /&gt;
Here is the basic structure of our gem: &lt;br /&gt;
&lt;br /&gt;
[[File:Structure.jpg|alt=Structure]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Lib''': Code from the automated_metareview package is placed in this folder. It contains all the 18 files.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.rb''' : This file is loaded when require’automated_metareview’ is run. This file is in charge of setting up your gem’s code and API.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.gemspec''' : This file gives details of the gem, author, version and other details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Build and Install Gem:''' &amp;lt;br/&amp;gt;&lt;br /&gt;
You can build a gem from gemspec using the following command:&lt;br /&gt;
          gem build automated_metareview.gemspec&lt;br /&gt;
Install the generated gem locally to test it out.&lt;br /&gt;
          gem install automated_metareview-0.0.2&lt;br /&gt;
&lt;br /&gt;
[[File:Build.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Push gem''' &amp;lt;br/&amp;gt;&lt;br /&gt;
In order to make this gem available to everyone, we need to push the gem using : &amp;lt;br/&amp;gt;&lt;br /&gt;
          gem push automated_metareview-0.0.2.gem  &lt;br /&gt;
&lt;br /&gt;
[[File:Push.jpg]]&lt;br /&gt;
&lt;br /&gt;
This  gem is now available for installation by anyone.[http://rubygems.org/gems/automated_metareview Automated Metareview Gem] &lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
You can check in the list of available gems:&amp;lt;br/&amp;gt;&lt;br /&gt;
          gem list -r automated_metareview&lt;br /&gt;
&lt;br /&gt;
[[File:List.jpg]]&lt;br /&gt;
&lt;br /&gt;
===Steps to install and use Gem===&lt;br /&gt;
&lt;br /&gt;
In order to use the gem please follow the below steps:&amp;lt;br/&amp;gt;&lt;br /&gt;
1. Run the following command to install the gem&lt;br /&gt;
        gem install automated_metareview-0.0.2&lt;br /&gt;
OR&lt;br /&gt;
Add following line in gemfile&lt;br /&gt;
        gem ‘automated_metareview-0.0.2’&lt;br /&gt;
Run following command: &lt;br /&gt;
        bundle install&lt;br /&gt;
2. Add following line in the code where you want to use the gem:&lt;br /&gt;
        require ‘automated_metareview-0.0.2’&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
In order to use automated_metareview feature of testing whether a phrase /word is suggestive or not, we can use SentenceState methods to test it :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Use.jpg]]&lt;br /&gt;
&lt;br /&gt;
To use the Experiza system Go to [http://152.7.99.90:5801/ Expertiza with Gem installed]&amp;lt;br/&amp;gt;&lt;br /&gt;
For detailed description about the steps to follow to test the automated metareview functionality Click [http://expertiza.ncsu.edu/submitted_content/download/25990?current_folder%5Bname%5D=%2Flocal%2Frails%2Fexpertiza%2Freleases%2F20131018020418%2Fpg_data%2Fefg%2Fcsc517%2Ff13%2Foss%2F3&amp;amp;download=README.docx here].&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
===Setup Issues===&lt;br /&gt;
1.    There were a lot of problems with the master branch of expertiza.&amp;lt;br&amp;gt;&lt;br /&gt;
2.    There were many missing routes.&amp;lt;br&amp;gt;&lt;br /&gt;
3.    We faced a number of problems installing Aspell dictionary on windows.&amp;lt;br&amp;gt;&lt;br /&gt;
4.    There are a number of steps which need to be followed to activate the metareview feature.&amp;lt;br&amp;gt;&lt;br /&gt;
5.    The automated meta review feature is partly implemented using java due to which there are a number of dependencies. To overcome these       dependencies we need to install multiple jar files such as joda-time.jar,stanford-segmenter.jar etc.&amp;lt;br&amp;gt;&lt;br /&gt;
6.    No concrete documentation about the working of the meta review functionality is available due to which a lot of time was required to get the feature running.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
1.[http://guides.rubygems.org/make-your-own-gem/ Make your own gem]&amp;lt;br&amp;gt;&lt;br /&gt;
2.[http://repository.lib.ncsu.edu/ir/handle/1840.16/8813 Automated Assessment of Reviews - Lakshmi Ramachandran ]&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81903</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81903"/>
		<updated>2013-10-31T01:51:10Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Steps to install and use Gem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed and issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The existing Expertiza system has an automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory has 18 files which provides features such as spell checker, plagiarism check and the review tone analysis.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We have converted automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system and include all those features in the Gem.Thus we can use the gem to include its functionality like review analysis etc.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The Automated metareview functionality is not used in the existing Expertiza system. If this feature is converted into a gem, then it can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Steps to create Gem===&lt;br /&gt;
&lt;br /&gt;
We have turned the automated_metareview folder into a gem called “automated_metareview” which could be used in other review systems.&lt;br /&gt;
Here is the basic structure of our gem: &lt;br /&gt;
&lt;br /&gt;
[[File:Structure.jpg|alt=Structure]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Lib''': Code from the automated_metareview package is placed in this folder. It contains all the 18 files.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.rb''' : This file is loaded when require’automated_metareview’ is run. This file is in charge of setting up your gem’s code and API.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.gemspec''' : This file gives details of the gem, author, version and other details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Build and Install Gem:''' &amp;lt;br/&amp;gt;&lt;br /&gt;
You can build a gem from gemspec using the following command:&lt;br /&gt;
          gem build automated_metareview.gemspec&lt;br /&gt;
Install the generated gem locally to test it out.&lt;br /&gt;
          gem install automated_metareview-0.0.2&lt;br /&gt;
&lt;br /&gt;
[[File:Build.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Push gem''' &amp;lt;br/&amp;gt;&lt;br /&gt;
In order to make this gem available to everyone, we need to push the gem using : &amp;lt;br/&amp;gt;&lt;br /&gt;
          gem push automated_metareview-0.0.2.gem  &lt;br /&gt;
&lt;br /&gt;
[[File:Push.jpg]]&lt;br /&gt;
&lt;br /&gt;
This  gem is now available for installation by anyone.[http://rubygems.org/gems/automated_metareview Automated Metareview Gem] &lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
You can check in the list of available gems:&amp;lt;br/&amp;gt;&lt;br /&gt;
          gem list -r automated_metareview&lt;br /&gt;
&lt;br /&gt;
[[File:List.jpg]]&lt;br /&gt;
&lt;br /&gt;
===Steps to install and use Gem===&lt;br /&gt;
&lt;br /&gt;
In order to use the gem please follow the below steps:&amp;lt;br/&amp;gt;&lt;br /&gt;
1. Run the following command to install the gem&lt;br /&gt;
        gem install automated_metareview-0.0.2&lt;br /&gt;
OR&lt;br /&gt;
Add following line in gemfile&lt;br /&gt;
        gem ‘automated_metareview-0.0.2’&lt;br /&gt;
Run following command: &lt;br /&gt;
        bundle install&lt;br /&gt;
2. Add following line in the code where you want to use the gem:&lt;br /&gt;
        require ‘automated_metareview-0.0.2’&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
In order to use automated_metareview feature of testing whether a phrase /word is suggestive or not, we can use SentenceState methods to test it :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Use.jpg]]&lt;br /&gt;
&lt;br /&gt;
To use the Experiza system Go to [http://152.7.99.90:5801/ Expertiza with Gem installed]&amp;lt;br/&amp;gt;&lt;br /&gt;
For detailed description about the steps to follow to test the automated metareview functionality Click [http://expertiza.ncsu.edu/submitted_content/download/25990?current_folder%5Bname%5D=%2Flocal%2Frails%2Fexpertiza%2Freleases%2F20131018020418%2Fpg_data%2Fefg%2Fcsc517%2Ff13%2Foss%2F3&amp;amp;download=README.docx here].&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
===Setup Issues===&lt;br /&gt;
1.    There were a lot of problems with the master branch of expertiza.&amp;lt;br&amp;gt;&lt;br /&gt;
2.    There were many missing routes.&amp;lt;br&amp;gt;&lt;br /&gt;
3.    We faced a number of problems installing Aspell dictionary on windows.&amp;lt;br&amp;gt;&lt;br /&gt;
4.    There are a number of steps which need to be followed to activate the metareview feature.&amp;lt;br&amp;gt;&lt;br /&gt;
5.    The automated meta review feature is partly implemented using java due to which there are a number of dependencies. To overcome these       dependencies we need to install multiple jar files such as joda-time.jar,stanford-segmenter.jar etc.&amp;lt;br&amp;gt;&lt;br /&gt;
6.    No concrete documentation about the working of the meta review functionality is available due to which a lot of time was required to get the feature running.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
1.[http://guides.rubygems.org/make-your-own-gem/ Make your own gem]&amp;lt;br&amp;gt;&lt;br /&gt;
2.[http://repository.lib.ncsu.edu/ir/handle/1840.16/8813 Automated Assessment of Reviews - Lakshmi Ramachandran ]&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81863</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81863"/>
		<updated>2013-10-31T01:42:48Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Steps to create Gem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed and issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The existing Expertiza system has an automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory has 18 files which provides features such as spell checker, plagiarism check and the review tone analysis.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We have converted automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system and include all those features in the Gem.Thus we can use the gem to include its functionality like review analysis etc.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The Automated metareview functionality is not used in the existing Expertiza system. If this feature is converted into a gem, then it can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Steps to create Gem===&lt;br /&gt;
&lt;br /&gt;
We have turned the automated_metareview folder into a gem called “automated_metareview” which could be used in other review systems.&lt;br /&gt;
Here is the basic structure of our gem: &lt;br /&gt;
&lt;br /&gt;
[[File:Structure.jpg|alt=Structure]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Lib''': Code from the automated_metareview package is placed in this folder. It contains all the 18 files.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.rb''' : This file is loaded when require’automated_metareview’ is run. This file is in charge of setting up your gem’s code and API.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.gemspec''' : This file gives details of the gem, author, version and other details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Build and Install Gem:''' &amp;lt;br/&amp;gt;&lt;br /&gt;
You can build a gem from gemspec using the following command:&lt;br /&gt;
          gem build automated_metareview.gemspec&lt;br /&gt;
Install the generated gem locally to test it out.&lt;br /&gt;
          gem install automated_metareview-0.0.2&lt;br /&gt;
&lt;br /&gt;
[[File:Build.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Push gem''' &amp;lt;br/&amp;gt;&lt;br /&gt;
In order to make this gem available to everyone, we need to push the gem using : &amp;lt;br/&amp;gt;&lt;br /&gt;
          gem push automated_metareview-0.0.2.gem  &lt;br /&gt;
&lt;br /&gt;
[[File:Push.jpg]]&lt;br /&gt;
&lt;br /&gt;
This  gem is now available for installation by anyone.[http://rubygems.org/gems/automated_metareview Automated Metareview Gem] &lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
You can check in the list of available gems:&amp;lt;br/&amp;gt;&lt;br /&gt;
          gem list -r automated_metareview&lt;br /&gt;
&lt;br /&gt;
[[File:List.jpg]]&lt;br /&gt;
&lt;br /&gt;
===Steps to install and use Gem===&lt;br /&gt;
&lt;br /&gt;
In order to use the gem please follow the below steps:&amp;lt;br/&amp;gt;&lt;br /&gt;
1. Run the following command to install the gem&lt;br /&gt;
        gem install automated_metareview-0.0.2&lt;br /&gt;
OR&lt;br /&gt;
Add following line in gemfile&lt;br /&gt;
        gem ‘automated_metareview-0.0.2’&lt;br /&gt;
Run following command: &lt;br /&gt;
        bundle install&lt;br /&gt;
2. Add following line in the code where you want to use the gem:&lt;br /&gt;
        require ‘automated_metareview-0.0.2’&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
In order to use automated_metareview feature of testing whether a phrase /word is suggestive or not, we can use SentenceState methods to test it :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Use.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
===Setup Issues===&lt;br /&gt;
1.    There were a lot of problems with the master branch of expertiza.&amp;lt;br&amp;gt;&lt;br /&gt;
2.    There were many missing routes.&amp;lt;br&amp;gt;&lt;br /&gt;
3.    We faced a number of problems installing Aspell dictionary on windows.&amp;lt;br&amp;gt;&lt;br /&gt;
4.    There are a number of steps which need to be followed to activate the metareview feature.&amp;lt;br&amp;gt;&lt;br /&gt;
5.    The automated meta review feature is partly implemented using java due to which there are a number of dependencies. To overcome these       dependencies we need to install multiple jar files such as joda-time.jar,stanford-segmenter.jar etc.&amp;lt;br&amp;gt;&lt;br /&gt;
6.    No concrete documentation about the working of the meta review functionality is available due to which a lot of time was required to get the feature running.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
1.[http://guides.rubygems.org/make-your-own-gem/ Make your own gem]&amp;lt;br&amp;gt;&lt;br /&gt;
2.[http://repository.lib.ncsu.edu/ir/handle/1840.16/8813 Automated Assessment of Reviews - Lakshmi Ramachandran ]&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81856</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81856"/>
		<updated>2013-10-31T01:41:52Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed and issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The existing Expertiza system has an automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory has 18 files which provides features such as spell checker, plagiarism check and the review tone analysis.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We have converted automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system and include all those features in the Gem.Thus we can use the gem to include its functionality like review analysis etc.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The Automated metareview functionality is not used in the existing Expertiza system. If this feature is converted into a gem, then it can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Steps to create Gem===&lt;br /&gt;
&lt;br /&gt;
We have turned the automated_metareview folder into a gem called “automated_metareview” which could be used in other review systems.&lt;br /&gt;
Here is the basic structure of our gem: &lt;br /&gt;
&lt;br /&gt;
[[File:Structure.jpg|alt=Structure]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Lib''': Code from the automated_metareview package is placed in this folder. It contains all the 18 files.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.rb''' : This file is loaded when require’automated_metareview’ is run. This file is in charge of setting up your gem’s code and API.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.gemspec''' : This file gives details of the gem, author, version and other details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Build and Install Gem:''' &amp;lt;br/&amp;gt;&lt;br /&gt;
You can build a gem from gemspec using the following command:&lt;br /&gt;
          gem build automated_metareview.gemspec&lt;br /&gt;
Install the generated gem locally to test it out.&lt;br /&gt;
          gem install automated_metareview-0.0.2&lt;br /&gt;
&lt;br /&gt;
[[File:Build.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Push gem''' &amp;lt;br/&amp;gt;&lt;br /&gt;
In order to make this gem available to everyone, we need to push the gem using : &amp;lt;br/&amp;gt;&lt;br /&gt;
          gem push automated_metareview-0.0.2.gem  &lt;br /&gt;
&lt;br /&gt;
[[File:Push.jpg]]&lt;br /&gt;
&lt;br /&gt;
This  gem is now available for installation by anyone.[http://rubygems.org/gems/automated_metareview Automated Metareview Gem] &amp;lt;ref&amp;gt;[http://rubygems.org/gems/automated_metareview Automated Metareview Gem structure]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
You can check in the list of available gems:&amp;lt;br/&amp;gt;&lt;br /&gt;
          gem list -r automated_metareview&lt;br /&gt;
&lt;br /&gt;
[[File:List.jpg]]&lt;br /&gt;
&lt;br /&gt;
===Steps to install and use Gem===&lt;br /&gt;
&lt;br /&gt;
In order to use the gem please follow the below steps:&amp;lt;br/&amp;gt;&lt;br /&gt;
1. Run the following command to install the gem&lt;br /&gt;
        gem install automated_metareview-0.0.2&lt;br /&gt;
OR&lt;br /&gt;
Add following line in gemfile&lt;br /&gt;
        gem ‘automated_metareview-0.0.2’&lt;br /&gt;
Run following command: &lt;br /&gt;
        bundle install&lt;br /&gt;
2. Add following line in the code where you want to use the gem:&lt;br /&gt;
        require ‘automated_metareview-0.0.2’&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
In order to use automated_metareview feature of testing whether a phrase /word is suggestive or not, we can use SentenceState methods to test it :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Use.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
===Setup Issues===&lt;br /&gt;
1.    There were a lot of problems with the master branch of expertiza.&amp;lt;br&amp;gt;&lt;br /&gt;
2.    There were many missing routes.&amp;lt;br&amp;gt;&lt;br /&gt;
3.    We faced a number of problems installing Aspell dictionary on windows.&amp;lt;br&amp;gt;&lt;br /&gt;
4.    There are a number of steps which need to be followed to activate the metareview feature.&amp;lt;br&amp;gt;&lt;br /&gt;
5.    The automated meta review feature is partly implemented using java due to which there are a number of dependencies. To overcome these       dependencies we need to install multiple jar files such as joda-time.jar,stanford-segmenter.jar etc.&amp;lt;br&amp;gt;&lt;br /&gt;
6.    No concrete documentation about the working of the meta review functionality is available due to which a lot of time was required to get the feature running.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
1.[http://guides.rubygems.org/make-your-own-gem/ Make your own gem]&amp;lt;br&amp;gt;&lt;br /&gt;
2.[http://repository.lib.ncsu.edu/ir/handle/1840.16/8813 Automated Assessment of Reviews - Lakshmi Ramachandran ]&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81852</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81852"/>
		<updated>2013-10-31T01:40:12Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Steps to create Gem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed and issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The existing Expertiza system has an automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory has 18 files which provides features such as spell checker, plagiarism check and the review tone analysis.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We have converted automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system and include all those features in the Gem.Thus we can use the gem to include its functionality like review analysis etc.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The Automated metareview functionality is not used in the existing Expertiza system. If this feature is converted into a gem, then it can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Steps to create Gem===&lt;br /&gt;
&lt;br /&gt;
We have turned the automated_metareview folder into a gem called “automated_metareview” which could be used in other review systems.&lt;br /&gt;
Here is the basic structure of our gem: &lt;br /&gt;
&lt;br /&gt;
[[File:Structure.jpg|alt=Structure]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Lib''': Code from the automated_metareview package is placed in this folder. It contains all the 18 files.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.rb''' : This file is loaded when require’automated_metareview’ is run. This file is in charge of setting up your gem’s code and API.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.gemspec''' : This file gives details of the gem, author, version and other details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Build and Install Gem:''' &amp;lt;br/&amp;gt;&lt;br /&gt;
You can build a gem from gemspec using the following command:&lt;br /&gt;
          gem build automated_metareview.gemspec&lt;br /&gt;
Install the generated gem locally to test it out.&lt;br /&gt;
          gem install automated_metareview-0.0.2&lt;br /&gt;
&lt;br /&gt;
[[File:Build.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Push gem''' &amp;lt;br/&amp;gt;&lt;br /&gt;
In order to make this gem available to everyone, we need to push the gem using : &amp;lt;br/&amp;gt;&lt;br /&gt;
          gem push automated_metareview-0.0.2.gem  &lt;br /&gt;
&lt;br /&gt;
[[File:Push.jpg]]&lt;br /&gt;
&lt;br /&gt;
This  gem is now available for installation by anyone. Automated Metareview Gem&amp;lt;ref&amp;gt;[http://rubygems.org/gems/automated_metareview Automated Metareview Gem structure]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
You can check in the list of available gems:&amp;lt;br/&amp;gt;&lt;br /&gt;
          gem list -r automated_metareview&lt;br /&gt;
&lt;br /&gt;
[[File:List.jpg]]&lt;br /&gt;
&lt;br /&gt;
===Steps to install and use Gem===&lt;br /&gt;
&lt;br /&gt;
In order to use the gem please follow the below steps:&amp;lt;br/&amp;gt;&lt;br /&gt;
1. Run the following command to install the gem&lt;br /&gt;
        gem install automated_metareview-0.0.2&lt;br /&gt;
OR&lt;br /&gt;
Add following line in gemfile&lt;br /&gt;
        gem ‘automated_metareview-0.0.2’&lt;br /&gt;
Run following command: &lt;br /&gt;
        bundle install&lt;br /&gt;
2. Add following line in the code where you want to use the gem:&lt;br /&gt;
        require ‘automated_metareview-0.0.2’&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
In order to use automated_metareview feature of testing whether a phrase /word is suggestive or not, we can use SentenceState methods to test it :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Use.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
===Setup Issues===&lt;br /&gt;
1.    There were a lot of problems with the master branch of expertiza.&amp;lt;br&amp;gt;&lt;br /&gt;
2.    There were many missing routes.&amp;lt;br&amp;gt;&lt;br /&gt;
3.    We faced a number of problems installing Aspell dictionary on windows.&amp;lt;br&amp;gt;&lt;br /&gt;
4.    There are a number of steps which need to be followed to activate the metareview feature.&amp;lt;br&amp;gt;&lt;br /&gt;
5.    The automated meta review feature is partly implemented using java due to which there are a number of dependencies. To overcome these       dependencies we need to install multiple jar files such as joda-time.jar,stanford-segmenter.jar etc.&amp;lt;br&amp;gt;&lt;br /&gt;
6.    No concrete documentation about the working of the meta review functionality is available due to which a lot of time was required to get the feature running.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
1.[http://guides.rubygems.org/make-your-own-gem/ Make your own gem]&amp;lt;br&amp;gt;&lt;br /&gt;
2.[http://repository.lib.ncsu.edu/ir/handle/1840.16/8813 Automated Assessment of Reviews - Lakshmi Ramachandran ]&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81843</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81843"/>
		<updated>2013-10-31T01:38:05Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Steps to create Gem */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed and issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
The existing Expertiza system has an automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory has 18 files which provides features such as spell checker, plagiarism check and the review tone analysis.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We have converted automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system and include all those features in the Gem.Thus we can use the gem to include its functionality like review analysis etc.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The Automated metareview functionality is not used in the existing Expertiza system. If this feature is converted into a gem, then it can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
===Steps to create Gem===&lt;br /&gt;
&lt;br /&gt;
We have turned the automated_metareview folder into a gem called “automated_metareview” which could be used in other review systems.&lt;br /&gt;
Here is the basic structure of our gem: &lt;br /&gt;
&lt;br /&gt;
[[File:Structure.jpg|alt=Structure]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Lib''': Code from the automated_metareview package is placed in this folder. It contains all the 18 files.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.rb''' : This file is loaded when require’automated_metareview’ is run. This file is in charge of setting up your gem’s code and API.&lt;br /&gt;
&lt;br /&gt;
'''automated_metareview.gemspec''' : This file gives details of the gem, author, version and other details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Build and Install Gem:''' &amp;lt;br/&amp;gt;&lt;br /&gt;
You can build a gem from gemspec using the following command:&lt;br /&gt;
          gem build automated_metareview.gemspec&lt;br /&gt;
Install the generated gem locally to test it out.&lt;br /&gt;
          gem install automated_metareview-0.0.2&lt;br /&gt;
&lt;br /&gt;
[[File:Build.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Push gem''' &amp;lt;br/&amp;gt;&lt;br /&gt;
In order to make this gem available to everyone, we need to push the gem using : &amp;lt;br/&amp;gt;&lt;br /&gt;
          gem push automated_metareview-0.0.2.gem  &lt;br /&gt;
&lt;br /&gt;
[[File:Push.jpg]]&lt;br /&gt;
&lt;br /&gt;
This  gem is now available for installation by anyone. This can be viewed in &lt;br /&gt;
&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://rubygems.org/gems/automated_metareview http://rubygems.org/gems/automated_metareview Automated Metareview Gem structure]&amp;lt;/ref&amp;gt; &lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
You can check in the list of available gems:&amp;lt;br/&amp;gt;&lt;br /&gt;
          gem list -r automated_metareview&lt;br /&gt;
&lt;br /&gt;
[[File:List.jpg]]&lt;br /&gt;
&lt;br /&gt;
===Steps to install and use Gem===&lt;br /&gt;
&lt;br /&gt;
In order to use the gem please follow the below steps:&amp;lt;br/&amp;gt;&lt;br /&gt;
1. Run the following command to install the gem&lt;br /&gt;
        gem install automated_metareview-0.0.2&lt;br /&gt;
OR&lt;br /&gt;
Add following line in gemfile&lt;br /&gt;
        gem ‘automated_metareview-0.0.2’&lt;br /&gt;
Run following command: &lt;br /&gt;
        bundle install&lt;br /&gt;
2. Add following line in the code where you want to use the gem:&lt;br /&gt;
        require ‘automated_metareview-0.0.2’&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
In order to use automated_metareview feature of testing whether a phrase /word is suggestive or not, we can use SentenceState methods to test it :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Use.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
===Setup Issues===&lt;br /&gt;
1.    There were a lot of problems with the master branch of expertiza.&amp;lt;br&amp;gt;&lt;br /&gt;
2.    There were many missing routes.&amp;lt;br&amp;gt;&lt;br /&gt;
3.    We faced a number of problems installing Aspell dictionary on windows.&amp;lt;br&amp;gt;&lt;br /&gt;
4.    There are a number of steps which need to be followed to activate the metareview feature.&amp;lt;br&amp;gt;&lt;br /&gt;
5.    The automated meta review feature is partly implemented using java due to which there are a number of dependencies. To overcome these       dependencies we need to install multiple jar files such as joda-time.jar,stanford-segmenter.jar etc.&amp;lt;br&amp;gt;&lt;br /&gt;
6.    No concrete documentation about the working of the meta review functionality is available due to which a lot of time was required to get the feature running.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
1.[http://guides.rubygems.org/make-your-own-gem/ Make your own gem]&amp;lt;br&amp;gt;&lt;br /&gt;
2.[http://repository.lib.ncsu.edu/ir/handle/1840.16/8813 Automated Assessment of Reviews - Lakshmi Ramachandran ]&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81168</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=81168"/>
		<updated>2013-10-30T19:22:56Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing Expertiza system had automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system. Include all those features in the Gem, and then import the functionality from the Gem file.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The features of Automated metareview were integrated in the existing Expertiza system. If these features were converted into a gem, then these can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
We have turned the automated_metareview feature into a gem called “automated_metareview” which could be used in other review systems.&lt;br /&gt;
Here is the basic structure of our gem: &lt;br /&gt;
&lt;br /&gt;
[[File:Structure.jpg|alt=Structure]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Lib''': code of automated_metareview package is placed in this folder. It contains all the 18 files.&lt;br /&gt;
&lt;br /&gt;
'''Automated_metareview.rb''' : This file gets loaded when require’automated_metareview’ is run. That one file is in charge of setting up your gem’s code and API.&lt;br /&gt;
&lt;br /&gt;
'''Automated_metareview.gemspec''' : This file gives details of the gem, author, version and other details.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Build and Install Gem:''' You can build a gem from gemspec. Then you can install the generated gem locally to test it out.&lt;br /&gt;
&lt;br /&gt;
[[File:Build.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Push gem'''  &lt;br /&gt;
&lt;br /&gt;
[[File:Push.jpg]]&lt;br /&gt;
&lt;br /&gt;
This  gem is now available for installation by anyone. This can be viewed in [http://rubygems.org/gems/automated_metareview http://rubygems.org/gems/automated_metareview] &lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
&lt;br /&gt;
You can check in the list of available gems:&lt;br /&gt;
&lt;br /&gt;
[[File:List.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In order to use the gem please follow the following steps:&lt;br /&gt;
1.           &lt;br /&gt;
	gem install automated_metareview-0.0.2&lt;br /&gt;
&lt;br /&gt;
OR&lt;br /&gt;
Add following line in gemfile&lt;br /&gt;
        gem ‘automated_metareview-0.0.2’&lt;br /&gt;
Run following command: &lt;br /&gt;
        bundle install.&lt;br /&gt;
2. Add following line in the code where you want to use the gem:&lt;br /&gt;
        require ‘automated_metareview-0.0.2’&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
[[File:Use.jpg]]&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix: Setup Issues==&lt;br /&gt;
1.    There were a lot of problems with the master branch of expertiza.&amp;lt;br&amp;gt;&lt;br /&gt;
2.    There were many missing routes.&amp;lt;br&amp;gt;&lt;br /&gt;
3.    We faced a number of problems installing Aspell dictionary on windows.&amp;lt;br&amp;gt;&lt;br /&gt;
4.    There are a number of steps which need to be followed to activate the metareview feature.&amp;lt;br&amp;gt;&lt;br /&gt;
5.    The automated meta review feature is partly implemented using java due to which there are a number of dependencies. To overcome these       dependencies we need to install multiple jar files such as joda-time.jar,stanford-segmenter.jar etc.&amp;lt;br&amp;gt;&lt;br /&gt;
6.    No concrete documentation about the working of the meta review functionality is available due to which a lot of time was required to get the feature running.&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80880</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80880"/>
		<updated>2013-10-30T17:44:27Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Future Work */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing Expertiza system had automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system. Include all those features in the Gem, and then import the functionality from the Gem file.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The features of Automated metareview were integrated in the existing Expertiza system. If these features were converted into a gem, then these can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
The current implementation has a few method calls very specific to Expertiza. We can change these methods so that they can be used in any other system. We generalized a few methods, so that it can be used in systems similar to Expertiza. But we can generalize it more such that we can seperate functionalities like Spell checker , plagiarism check in separate gem file.&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80831</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80831"/>
		<updated>2013-10-30T17:13:31Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing Expertiza system had automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system. Include all those features in the Gem, and then import the functionality from the Gem file.&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The features of Automated metareview were integrated in the existing Expertiza system. If these features were converted into a gem, then these can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80830</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80830"/>
		<updated>2013-10-30T17:11:52Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing Expertiza system had automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system. Include all those features in the Gem, and then import the functionality from the Gem file.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The features of Automated metareview were integrated in the existing Expertiza system. If these features were converted into a gem, then these can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80829</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80829"/>
		<updated>2013-10-30T17:11:14Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Motivation */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing Expertiza system had automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system. Include all those features in the Gem, and then import the functionality from the Gem file.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
The features of Automated metareview were intgerated in the existing Expertiza system. If these features were converted into a gem, then these can be used in other review systems, wherein the reviewer will get an automated feedback about the review and he/she can change the review based on the results of automated metreview results. Thus by creating a gem file, other systems can install the gem and get access to all features of automated metareview, thereby increasing reusability.&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80798</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80798"/>
		<updated>2013-10-30T16:50:25Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing Expertiza system had automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system. Include all those features in the Gem, and then import the functionality from the Gem file.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80797</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80797"/>
		<updated>2013-10-30T16:48:54Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing system had automated_metareview.rb class which was using the text processing functionality from automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
Our goal was to remove the automated_metareview directory from existing Expertiza system. Include all those features in the Gem, and then import the functionality from the Gem file.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80785</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80785"/>
		<updated>2013-10-30T16:43:35Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
Existing system had automated_metareview.rb class which was using the text processing functionality from the in the automated_metareview directory. &amp;quot;automated_metareview&amp;quot; directory had 18 files which included features like spell checker, plagiarism check and the review tone.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80774</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80774"/>
		<updated>2013-10-30T16:38:08Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
We had the following code already implemented.&amp;lt;br/&amp;gt;&lt;br /&gt;
Class: automated_metareview.rb (188 lines) and 18 files in the automated_metareview directory.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What it does''':  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
'''What needs to be done''': We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80773</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80773"/>
		<updated>2013-10-30T16:37:38Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
We had the following code already implemented.&amp;lt;br/&amp;gt;&lt;br /&gt;
Class: automated_metareview.rb (188 lines) and 18 files in the automated_metareview directory.&amp;lt;br/&amp;gt;&lt;br /&gt;
What it does:  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
What needs to be done: We had to convert automated_metareview directory into a gem so that it can be used with other review systems.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80770</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80770"/>
		<updated>2013-10-30T16:36:54Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
We had the following code already implemented.&amp;lt;br/&amp;gt;&lt;br /&gt;
Class: automated_metareview.rb (188 lines) and 18 files in the automated_metareview directory.&amp;lt;br/&amp;gt;&lt;br /&gt;
What it does:  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&amp;lt;br/&amp;gt;&lt;br /&gt;
What needs to be done: We had to convert automated_metareview directory into a gem so that it can be used with other system.&amp;lt;br/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80769</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80769"/>
		<updated>2013-10-30T16:36:10Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Project Description */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
We had the following code already implemented.&lt;br /&gt;
Class: automated_metareview.rb (188 lines) and 18 files in the automated_metareview directory.&lt;br /&gt;
What it does:  Gives automatic feedback to a reviewer, using NLP text analysis, to help the reviewer improve the review.&lt;br /&gt;
What needs to be done: We had to convert automated_metareview directory into a gem so that it can be used with other system.&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80762</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80762"/>
		<updated>2013-10-30T16:24:06Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Intrdouction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
This article is a report about our Open Source project(Expertiza) - Turn automated_metareview folder into a gem . It explains the design considerations, steps followed, issues faced while setting up Expertiza. It explains how our team manages to create a new Gem file, and test it on the existing Expertiza system.&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80761</id>
		<title>CSC/ECE 517 Fall 2013/oss E812 amp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/oss_E812_amp&amp;diff=80761"/>
		<updated>2013-10-30T16:18:36Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: Created page with &amp;quot;==Intrdouction==   ==Project Description==   ==Design==   ==Motivation==   ==Future Work==   ==Appendix: setup issues==&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Intrdouction==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Project Description==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Design==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Motivation==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Future Work==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Appendix: setup issues==&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013&amp;diff=80757</id>
		<title>CSC/ECE 517 Fall 2013</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013&amp;diff=80757"/>
		<updated>2013-10-30T16:07:44Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;* [[ CSC/ECE 517 Fall 2012/ch1 1w23 ph ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w30 nn]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w21 w]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w01 aj]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w24 nv]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w29 rkld]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w25 aras]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w30 ps]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w19 rj]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w18 bs]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w17 pk]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w22 ss]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w12 vn]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w14 st]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w08 cc]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w10 ga ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w26 as ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w27 ma ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w13 aa ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w11 sv ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w07 d ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w20 gq ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w03 ss ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w28 nm ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w02 pp ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1  1w6 zs ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1  1w04 y ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w05 st ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w09 hs ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w32 av ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w48 x ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w43 av]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w46 ka]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w33 aa]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w35 sa ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w39 as ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w31 vm ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w43 sm ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w44 s ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w47 ka ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w34 fs ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch1 1w40 ao ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss fmv ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss vna ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss paa ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss mapFeeds ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss ssp ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/ch2 0e808 nsv ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss aoa ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss cmh ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss ans ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss ssv]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss E818 mra ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss SocialMediaFeeds ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss E811 syn ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss E804 spb ]]&lt;br /&gt;
* [[ CSC/ECE 517 Fall 2013/oss E812 amp ]]&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78713</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78713"/>
		<updated>2013-09-24T15:57:07Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmarking */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref name='w'&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Benchmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Benchmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Benchmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional test cases in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce unexpected results. [http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78712</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78712"/>
		<updated>2013-09-24T15:56:38Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmark.measure method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref name='w'&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Benchmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional test cases in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce unexpected results. [http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78711</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78711"/>
		<updated>2013-09-24T15:55:49Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Introduction to Profiling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref name='w'&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional test cases in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce unexpected results. [http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78709</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78709"/>
		<updated>2013-09-24T15:55:00Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional test cases in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce unexpected results. [http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78708</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78708"/>
		<updated>2013-09-24T15:52:49Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results. [http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78707</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78707"/>
		<updated>2013-09-24T15:52:36Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78705</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78705"/>
		<updated>2013-09-24T15:50:03Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78704</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78704"/>
		<updated>2013-09-24T15:49:27Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references&amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;/references&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78701</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78701"/>
		<updated>2013-09-24T15:48:35Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78700</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78700"/>
		<updated>2013-09-24T15:48:23Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;br /&gt;
&amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimization Tutorial]&amp;lt;ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78696</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78696"/>
		<updated>2013-09-24T15:40:35Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Ruby-prof Profiler */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref name='in'&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78695</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78695"/>
		<updated>2013-09-24T15:37:39Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmarking */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm &amp;lt;ref name='be'&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78693</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78693"/>
		<updated>2013-09-24T15:32:16Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Ruby-prof Profiler */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &amp;lt;ref&amp;gt;[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;/ref&amp;gt; &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance. For more examples on benchmarking refer &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78691</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78691"/>
		<updated>2013-09-24T15:28:53Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Optimizing Ruby Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance. For more examples on benchmarking refer &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
'''Ruby Inline''' &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section6.html Ruby Inline]&amp;lt;/ref&amp;gt; allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78683</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78683"/>
		<updated>2013-09-24T15:23:41Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmark.bmbm method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance. For more examples on benchmarking refer &amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhmark examples]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78681</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78681"/>
		<updated>2013-09-24T15:23:07Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmark.bmbm method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance. For more examples on benchmarking refer&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhamrk examples]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78680</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78680"/>
		<updated>2013-09-24T15:22:38Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmark.bmbm method */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance. For more examples on benchmarking refer&amp;lt;ref&amp;gt;[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhamrk examples]&amp;lt;/rel&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code==&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78675</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78675"/>
		<updated>2013-09-24T15:20:02Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Default Profiler Profiling and optimizing Ruby code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code using profile.rb(default profiler)]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78673</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78673"/>
		<updated>2013-09-24T15:19:16Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Ruby Profiling tools */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code]&amp;lt;/ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78670</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78670"/>
		<updated>2013-09-24T15:18:45Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Default Profiler */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler &amp;lt;ref&amp;gt;[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code]&amp;lt;ref&amp;gt;===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78664</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78664"/>
		<updated>2013-09-24T15:15:54Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmarking */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;ref&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/ref&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage collector. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78658</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78658"/>
		<updated>2013-09-24T15:12:32Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Benchmarking */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking&amp;lt;rel&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/rel&amp;gt; can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm&amp;lt;rel&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/rel&amp;gt; method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm&amp;lt;rel&amp;gt;[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;/rel&amp;gt; This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage allocator. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78652</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78652"/>
		<updated>2013-09-24T15:03:50Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Introduction to Profiling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm. This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage allocator. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78651</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78651"/>
		<updated>2013-09-24T15:03:22Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;https://www.ruby-lang.org/en/about&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm. This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage allocator. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78650</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78650"/>
		<updated>2013-09-24T15:02:07Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: /* Introduction to Profiling */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &amp;lt;ref&amp;gt;https://www.ruby-lang.org/en/about&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm. This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage allocator. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhamrk examples]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pic.dhe.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.prftools%2Fdoc%2Fprftools%2Feventbasedprofiling.htm Event-based profiling]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.synbioz.com/blog/optimize_ruby_code Introduction to Ruby code optimization]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimaization Tutorial]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:NewProfiling.jpg&amp;diff=78631</id>
		<title>File:NewProfiling.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:NewProfiling.jpg&amp;diff=78631"/>
		<updated>2013-09-24T14:33:05Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78630</id>
		<title>CSC/ECE 517 Fall 2013/ch1 1w02 pp</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2013/ch1_1w02_pp&amp;diff=78630"/>
		<updated>2013-09-24T14:31:27Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction to Profiling==&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profiling] in general, is an important feature in computer science. It is a technique, in the most basic terms, by which one can analyze the efficiency of the program or code by measuring the program’s time complexity or space complexity and some other performance related parameters. For example, 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.&lt;br /&gt;
Profiling takes into consideration significant steps or program constructs (like loop statements, individual blocks and modules, statements involving operations and functions like aggregate functions) for performance study. &lt;br /&gt;
&lt;br /&gt;
'''Ruby profiling''' is analysis of Ruby programs. We make use of profiles that are programs, which takes ruby code as input and on execution gives values for a set of parameters that define the performance of the input ruby program. There are many types of profiling methods 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.&lt;br /&gt;
&lt;br /&gt;
[[Image:NewProfiling.jpg|frame|center|Ruby Profiling Tools]]&lt;br /&gt;
&lt;br /&gt;
As we can see in the flow diagram, we give the ruby program as input to the Ruby profiling tools and we get a profiler output, which helps us in optimizing out code.&lt;br /&gt;
&lt;br /&gt;
==Significance of Profiling==&lt;br /&gt;
The main purpose of profiling is:&lt;br /&gt;
* To analyze the performance of the code, their CPU utilization, memory operations.&lt;br /&gt;
&lt;br /&gt;
* 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. &lt;br /&gt;
&lt;br /&gt;
* We can accurately measure how a program functions or performs in one given environment and with different input data.&lt;br /&gt;
&lt;br /&gt;
* To identify the bottlenecks in a program, the portions that cause program overhead or can slow down the system or need special testing or can raise exception.&lt;br /&gt;
&lt;br /&gt;
==Ruby Profiling tools==&lt;br /&gt;
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 Default profiler and Benchmark module. Also, there is ruby-prof profiler which is a gem file developed to profile ruby code. Below is a description of how one can use these tools to profile ruby codes.&lt;br /&gt;
&lt;br /&gt;
===Default Profiler===&lt;br /&gt;
This is an inbuilt module that can be run by using the command –r profile which in turn imports the profile.rb source file. The 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.&lt;br /&gt;
&lt;br /&gt;
How to use profile.rb to profile our code:&amp;lt;br&amp;gt;&lt;br /&gt;
1) Create a ruby file profiler_example.rb&lt;br /&gt;
  #profile_example.rb code begins&lt;br /&gt;
  require 'profile'&lt;br /&gt;
  def slow_method&lt;br /&gt;
    10000.times do 9999999999999999*999999999&lt;br /&gt;
     end&lt;br /&gt;
   end&lt;br /&gt;
   slow_method&lt;br /&gt;
  #profile_example.rb code ends&lt;br /&gt;
Here, we have declared a function slow_method to simply perform multiplication of two values 10000 times using &amp;quot;times&amp;quot; iterator.&lt;br /&gt;
&lt;br /&gt;
2. Run the default profiler on profiler_example.rb using command&lt;br /&gt;
  ruby-rprofile profiler_example.rb&lt;br /&gt;
&lt;br /&gt;
3. Output of profiler&lt;br /&gt;
  %   cumulative   self              self     total&lt;br /&gt;
 time   seconds   seconds    calls  ms/call  ms/call  name&lt;br /&gt;
 65.96     0.06      0.06        1    62.00    94.00  Integer#times&lt;br /&gt;
 34.04     0.09      0.03    10000     0.00     0.00  Bignum#*&lt;br /&gt;
  0.00     0.09      0.00        1     0.00     0.00  Array#each&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  Object#slow_method&lt;br /&gt;
  0.00     0.09      0.00        1     0.00    94.00  #toplevel&amp;lt;br&amp;gt;&lt;br /&gt;
  '''Note:''' This output is truncated to show basic functions in ruby. When we run this code, we see a lot of other functions.&lt;br /&gt;
&lt;br /&gt;
Basically, this is considered as an inefficient profiling tool as this increases the execution time of program considerably and the situation worsens if the code size is huge. So we can use this to profile small pieces of code. &lt;br /&gt;
&lt;br /&gt;
But on the other hand, it’s a handy tool as it comes with the ruby library and with the –r command line, it becomes even more convenient, so this tool can certainly be used with the small length codes.  Also, this profiler provides great uniformity to the code. We can be sure of the accuracy of the result as while it measures the execution time of each function call and method, it slows down everything to the same level.&lt;br /&gt;
&lt;br /&gt;
Now the output of analysis is displayed in the form of table. Each row represents one method of the program. And the rows are in the sorted order of one of the parameters. It does take into account the time a method takes to run and computes the total as time taken by operation multiplied by number of times operations(or methods) is executed.&lt;br /&gt;
&lt;br /&gt;
===Ruby-prof Profiler===&lt;br /&gt;
[http://ruby-prof.rubyforge.org/ '''ruby-prof'''] is a fast code profiler for Ruby. It is a C extension and therefore is many times faster than the standard Ruby profiler. It supports both flat and graph profiles. For each method, graph profiles show how long the method ran, which methods called it and which all methods were called by it. RubyProf generates both text and html and can output it to a file.&lt;br /&gt;
&lt;br /&gt;
'''To demonstrate use of ruby-prof profiler we will use flat profile, Steps to be followed :'''&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''1.''' Install ruby-prof gem &lt;br /&gt;
  gem install ruby-prof&lt;br /&gt;
'''2.''' Create a ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
  #Start of ruby code file to profile&lt;br /&gt;
  require 'ruby-prof'&lt;br /&gt;
  RubyProf.start&lt;br /&gt;
  #code to profile start&lt;br /&gt;
  def slow_method&lt;br /&gt;
    for num in 1..10_000 do&lt;br /&gt;
      is_prime = 1&lt;br /&gt;
      for x in 2..(num - 1) do&lt;br /&gt;
        if (num % x == 0)&lt;br /&gt;
          is_prime = x&lt;br /&gt;
          break&lt;br /&gt;
        end&lt;br /&gt;
      end&lt;br /&gt;
      if is_prime == 1&lt;br /&gt;
        puts &amp;quot;#{num} is a prime number&amp;quot;&lt;br /&gt;
      else&lt;br /&gt;
        puts &amp;quot;#{num} equals #{is_prime} * #{num/is_prime}&amp;quot;&lt;br /&gt;
      end&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
  slow_method&lt;br /&gt;
  #code to profile end&lt;br /&gt;
  result = RubyProf.stop&lt;br /&gt;
  printer = RubyProf::FlatPrinter.new(result)&lt;br /&gt;
  printer.print(STDOUT)&lt;br /&gt;
  #End of ruby code file to profile&lt;br /&gt;
&lt;br /&gt;
'''3.''' Output of ruby-prof profiler&lt;br /&gt;
&lt;br /&gt;
  Thread ID: 17223036&lt;br /&gt;
  Fiber ID: 20361900&lt;br /&gt;
  Total: 1.873907&lt;br /&gt;
  Sort by: self_time&lt;br /&gt;
&lt;br /&gt;
 %self      total      self      wait     child    calls   name&lt;br /&gt;
  2.70      0.051     0.051     0.000     0.000    20000   IO#write&lt;br /&gt;
  1.59      1.874     0.030     0.000     1.844    10001  *Range#each&lt;br /&gt;
  0.47      0.009     0.009     0.000     0.000    27540   Fixnum#to_s&lt;br /&gt;
  0.39      0.058     0.007     0.000     0.051    10000   IO#puts&lt;br /&gt;
  0.21      0.004     0.004     0.000     0.000    10000   &amp;lt;Class::Range&amp;gt;#allocate&lt;br /&gt;
  0.20      0.062     0.004     0.000     0.058    10000   Kernel#puts&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Global#[No method]&lt;br /&gt;
  0.00      1.874     0.000     0.000     1.874        1   Object#slow_method&lt;br /&gt;
  0.00      0.000     0.000     0.000     0.000        1   Module#method_added&lt;br /&gt;
&lt;br /&gt;
The above code displays all the functions that were called and their execution time sorted in descending order of time taken by them. So, we find the method that consumes a lot of time at the top.&lt;br /&gt;
&lt;br /&gt;
===Benchmarking===&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be taken as a profiling tool as it provides measurement results after executing the program in question. Based on the analysis results, one can optimize the code accordingly.  All we need to do to use this module is to include benchmark, the step goes as, “require ‘benchmark’. The output from benchmark analysis consists of four main parameters shown from left to right- &lt;br /&gt;
&lt;br /&gt;
  User    system   total    real&lt;br /&gt;
&lt;br /&gt;
User CPU Time - Time taken to process user code instructions.&amp;lt;br&amp;gt;                                                             &lt;br /&gt;
System CPU time - Time taken to execute kernel code instructions.&amp;lt;br&amp;gt;                                                         &lt;br /&gt;
Total - User + system CPU time.&amp;lt;br&amp;gt;                                                                                                                              Total Real time - Time from the start of code execution to the end of code execution measured by clock time.&amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
In order to measure a part of the code, we can use many methods given in Benchmark. Below are the methods that are used to benchmark code.&lt;br /&gt;
&lt;br /&gt;
'''Illustrating these methods with an example'''&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.measure method'''====&lt;br /&gt;
Benchmark.measure is used to measure a part of the code, we can use many methods given in Benchmark. Here only one line at a time can be benchmarked.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  require 'benchmark'&lt;br /&gt;
  puts Benchmark.measure { 602214.times { 3.14159 * 6.626068 } }&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
   0.062000   0.000000   0.062000 (  0.056038)&lt;br /&gt;
&lt;br /&gt;
====Benchmark.bm method'''====&lt;br /&gt;
Benchmark.bm method is used to benchmark multiple lines of code. We call the method benchmark bm that takes block as input. The block variable is a special object that has report method, which in turn gives the execution time of the block executed.&lt;br /&gt;
&lt;br /&gt;
 #Code&lt;br /&gt;
 Benchmark.bm do |x|&lt;br /&gt;
   x.report(&amp;quot;for:&amp;quot;)   { for i in 1..n; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;times:&amp;quot;) { n.times do   ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
   x.report(&amp;quot;upto:&amp;quot;)  { 1.upto(n) do ; a = &amp;quot;1&amp;quot;; end }&lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
 #Output&lt;br /&gt;
           user     system      total        real&lt;br /&gt;
 for:    3.167000   0.000000   3.167000 (  3.312189)&lt;br /&gt;
 times:  3.198000   0.000000   3.198000 (  3.242186)&lt;br /&gt;
 upto:   3.884000   0.000000   3.884000 (  3.934225)&lt;br /&gt;
&lt;br /&gt;
===='''Benchmark.bmbm method'''====&lt;br /&gt;
Benchmark.bmbm. This method is similar to benchmark bm but it benchmarks or analyses the code twice. The objects in the initial phase are busy with memory allocation or interacting with garbage allocator. So, in the first run, it initializes all variables and forces garbage collector to run and in second run, it measures the code efficiency. The 1st phase is called rehearsal.  So with this method, Ruby first comfortably settles down with allocation task and then measures CPU time and other important parameters.&lt;br /&gt;
&lt;br /&gt;
  #Code&lt;br /&gt;
  array = (1..1000000).map { rand }&lt;br /&gt;
  Benchmark.bmbm do |x|&lt;br /&gt;
    x.report(&amp;quot;sort!&amp;quot;) { array.dup.sort! }&lt;br /&gt;
    x.report(&amp;quot;sort&amp;quot;)  { array.dup.sort  }&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  #Output&lt;br /&gt;
  Rehearsal -----------------------------------------&lt;br /&gt;
  sort!   6.069000   0.015000   6.084000 (  6.382365)&lt;br /&gt;
  sort    4.617000   0.000000   4.617000 (  4.724270)&lt;br /&gt;
  ------------------------------- total: 10.701000sec&lt;br /&gt;
  user     system      total        real&lt;br /&gt;
  sort!   3.323000   0.000000   3.323000 (  3.590205)&lt;br /&gt;
  sort    3.135000   0.000000   3.135000 (  3.267187)&lt;br /&gt;
&lt;br /&gt;
Benchmarking can be used in places where we have several algorithms for a single problem and we want to test the algorithm in different environment. In this case, each code can be formulated and tested using benchmark. Finally, we get comparisons with respect to real running time.  We just need to include benchmarking script that compares various approaches for performance.&lt;br /&gt;
&lt;br /&gt;
==Optimizing Ruby Code&lt;br /&gt;
Now we have seen how to analyze a code using ruby profiling tools. Now we will see the techniques we can use to optimize our code. Using benchmarking we can compare different algorithms and then use the most efficient algorithm. As we saw in the example of benchmarking, we compared for, upto and times and based on the results of comparison, we narrowed down on the method which has the least execution time. We can also use Ruby-Inline which reduces the execution time of program by a huge factor.&lt;br /&gt;
&lt;br /&gt;
Ruby Inline allows you to write C/Java within your ruby code. It automatically determines if the code in question has changed or not and builds it only when necessary. When we run profilers, we get the time taken by methods/algorithms to execute. Based on it, we can rewrite the methods in C. When we again run the profiler to test the performance, we get surprising results because the execution time reduces. &lt;br /&gt;
* Example - how our code will look &lt;br /&gt;
&lt;br /&gt;
 class MyTest&lt;br /&gt;
 def factorial(n)&lt;br /&gt;
   f = 1&lt;br /&gt;
   n.downto(2) { |x| f *= x }&lt;br /&gt;
   f&lt;br /&gt;
 end&lt;br /&gt;
 inline :C do |builder|&lt;br /&gt;
   builder.c &amp;quot;&lt;br /&gt;
   long factorial_c(int max) {&lt;br /&gt;
     int i=max, result=1;&lt;br /&gt;
     while (i &amp;gt;= 2) { result *= i--; }&lt;br /&gt;
     return result;&lt;br /&gt;
   }&amp;quot;&lt;br /&gt;
  end   &lt;br /&gt;
 end&lt;br /&gt;
&lt;br /&gt;
To install RubyInline go to [http://danlynn.org/home/2012/1/15/ruby-optimized-using-inline-c-and-java.html Install Ruby-Inline].&amp;lt;br&amp;gt;&lt;br /&gt;
Also you can go through the [http://www.who-t.net/tips/doku.php?id=ruby_inline_tutorial Ruby-Inline Tutorial].&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
For large scale applications, where there are thousands of users and transactions, try using Ruby optimizing techniques to optimize the ruby code and improve the performance. Optimization is a special form of refactoring. As such, it is important that you have a good set of unit and functional tests in place before you start optimizing your code. You might want your code to be faster, but you certainly don't want it to produce different results.&lt;br /&gt;
&lt;br /&gt;
==Realted Topics==&lt;br /&gt;
[http://newrelic.com/ Profiling Ruby on Rails using New Relic]&amp;lt;br&amp;gt;&lt;br /&gt;
[https://www.ruby-toolbox.com/resources/780 Ruby on Rails profiling tools]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
[http://en.wikipedia.org/wiki/Profiling_(computer_programming) Profling]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://rubylearning.com/blog/2013/06/19/how-do-i-benchmark-ruby-code/ Benchmark Ruby Code]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ruby-doc.org/stdlib-2.0/libdoc/benchmark/rdoc/Benchmark.html Becnhamrk examples]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://pic.dhe.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.prftools%2Fdoc%2Fprftools%2Feventbasedprofiling.htm Event-based profiling]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/section3.html Profiling and optimizing Ruby code]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.synbioz.com/blog/optimize_ruby_code Introduction to Ruby code optimization]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://wiki.vpslink.com/HOWTO:_installing,_updating_and_removing_rubygems#Updating HOWTO: INSTALLING, UPDATING AND REMOVING RUBYGEMS]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://www.ibm.com/developerworks/opensource/tutorials/os-ruby2/ Ruby code optimaization Tutorial]&amp;lt;br&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:RubyProfiling.jpg&amp;diff=78629</id>
		<title>File:RubyProfiling.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:RubyProfiling.jpg&amp;diff=78629"/>
		<updated>2013-09-24T14:26:50Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: uploaded a new version of &amp;amp;quot;File:RubyProfiling.jpg&amp;amp;quot;: Reverted to version as of 14:23, 24 September 2013&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Process of profiling ruby code&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:RubyProfiling.jpg&amp;diff=78628</id>
		<title>File:RubyProfiling.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:RubyProfiling.jpg&amp;diff=78628"/>
		<updated>2013-09-24T14:26:45Z</updated>

		<summary type="html">&lt;p&gt;Pkadam: uploaded a new version of &amp;amp;quot;File:RubyProfiling.jpg&amp;amp;quot;: Reverted to version as of 14:23, 24 September 2013&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Process of profiling ruby code&lt;/div&gt;</summary>
		<author><name>Pkadam</name></author>
	</entry>
</feed>