Ruby Code Optimizations (for beginners)
-
Upload
chirantanrajhans -
Category
Technology
-
view
1.963 -
download
3
description
Transcript of Ruby Code Optimizations (for beginners)
![Page 1: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/1.jpg)
1
Code OptimizationsRuby
28 minutes
![Page 2: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/2.jpg)
2
Huh? What’s that?
Maintenance
•Simple•Extensible•Pertaining to the design principlesPerfo
rmance
•Speed•Memory
![Page 3: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/3.jpg)
3
Why would I want to do that?
Money Resources▪ Hardware▪ People (Developers)
Time▪ Speed of operation▪ Developer takes to figure out what’s going on
Karma
No one wants to write bad code!
![Page 4: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/4.jpg)
4
Ruby
![Page 5: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/5.jpg)
5
Interpolation over Concatenation
puts "This string embeds #{a} and #{b} through interpolation" # => faster
puts "This string concatenates " << a << " and " << b # => slower
puts "This string concatenates “ + a + " and " + b # => slower
![Page 6: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/6.jpg)
6
Destructive Operations!
hash = {}hash = hash.merge({1 => 2}) # duplicates the original hashhash.merge!({1 => 2}) # equivalent to previous line, and faster
str = "string to gsub"str = str.gsub(/to/, 'copy') # duplicate string and reassigns itstr.gsub!(/to/, 'copy') # same effect, but no object duplication
![Page 7: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/7.jpg)
7
Benchmark everything
require 'benchmark'
n = 100000Benchmark.bm do |x|
x.report('copy') { n.times do ; h = {}; h = h.merge({1 => 2}); end } x.report('no copy') { n.times do ; h = {}; h.merge!({1 => 2}); end }
end
# user system total real# copy 0.460000 0.180000 0.640000
(0.640692)# no copy 0.340000 0.120000 0.460000
(0.463339)
![Page 8: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/8.jpg)
8
Symbols whenever possible
'foo'.object_id # => 23233310'foo'.object_id # => 23228920
:foo.object_id # => 239298:foo.object_id # => 239298
![Page 9: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/9.jpg)
9
Array joins
[1, 2, 3] * 3 # => [1, 2, 3, 1, 2, 3, 1, 2, 3]
[1, 2, 3] * '3' # => "13233"
%w{this is a test} * ", " # => "this, is, a, test"
h = { :name => "Fred", :age => 77 }h.map { |i| i * "=" } * "\n" # => "age=77\nname=Fred"
![Page 10: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/10.jpg)
10
Everything is an object!
def add(adder, addee) adder + addeeend
add(3,5) # => 8
class Fixnum def add(num) self + num endend
3.add(5) # => 8
![Page 11: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/11.jpg)
11
Use ranges instead of complex comparisons for numbers
# No more if x > 1000 && x < 2000 nonsense. Instead:
year = 1972puts case year when 1970..1979: "Seventies" when 1980..1989: "Eighties" when 1990..1999: "Nineties" end
![Page 12: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/12.jpg)
12
Ruby logic
def is_odd(x) if x % 2 == 0 return false else return true endend
def is_odd(x) x % 2 == 0 ? false : trueend
def is_odd(x) x % 2 != 0end
class Fixnum def odd? self % 2 != 0 endend
2.odd? # => false
![Page 13: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/13.jpg)
13
Enumerate single object
# [*items] converts a single object into an array. And if the object is an array, keeps it as it is.
[*items].each do |item| # ...end
![Page 14: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/14.jpg)
14
Rails
![Page 15: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/15.jpg)
15
Caching
Page Caching Fragment Caching Action Caching
Caching into Local / Instance Variable
# Makes a database query only if @current_user is nildef current_user @current_user ||= User.find(session[:user_id])end
![Page 16: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/16.jpg)
16
Don’t limit yourself to ActiveRecord
Use performance boosting database
features like:
Stored proceduresFunctionsX-query
![Page 17: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/17.jpg)
17
Finders are great but be careful
Retrieve only the information that you need.
Don’t kill your database with too many queries. Use
eager loading.
Avoid dynamic finders like MyModel.find_by_*.
Need an optimized query? Run MyModel.find_by_sql.
# This will generates only one query,# rather than Post.count + 1 queriesfor post in Post.find(:all, :include => [ :author, :comments ]) # Do something with postend
![Page 18: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/18.jpg)
18
Control your controllers
Don’t let them become the God
classes.
Lesser instance variables.
Slimmer the better.
Appropriate code in appropriate
controller.
![Page 19: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/19.jpg)
19
And of course!
![Page 20: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/20.jpg)
20
Obey design principles.
Always code in context (Objects). It
is the best way to model your
solution.
Avoid “Quick and dirty”.
Understand that no good can ever
come out of duplication. Be it code,
design, data and most importantly
effort.
Simplicity is the key.
Enjoy coding!
![Page 21: Ruby Code Optimizations (for beginners)](https://reader038.fdocuments.in/reader038/viewer/2022110309/558c1888d8b42add718b4648/html5/thumbnails/21.jpg)
21
Slides
slideshare.net/ihower/rails-best-practices
slideshare.net/ihower/practical-rails2-350619
slideshare.net/preston.lee/logical-programming-with-ru
by-prolog
Ruby Books
The Ruby Way by Hal Edwin Fulton, Guy Hurst
The Rails Way by Obie Fernandez
Design Patterns in Ruby by Russ Olsen
Software Construction
Code Complete 2 by Steve McConnell
The Pragmatic Programmer: From Journeyman to Master by
Andrew Hunt