Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.
Code Optimizations<br />Ruby<br />1<br />28 minutes<br />
Huh? What’s that?<br />2<br />
Why would I want to do that?<br />Money<br />Resources<br />Hardware<br />People (Developers)<br />Time<br />Speed of oper...
4<br />Ruby<br />
Interpolation over Concatenation<br />puts "This string embeds #{a} and #{b} through interpolation"# => faster<br />puts "...
Destructive Operations!<br />hash = {}<br />hash =hash.merge({1=>2}) <br /># duplicates the original hash<br />hash.merge!...
Benchmark everything<br />require 'benchmark'<br />n =100000<br />Benchmark.bm do |x| <br />x.report('copy') { n.timesdo ;...
Symbols whenever possible<br />'foo'.object_id# => 23233310<br />'foo'.object_id# => 23228920<br />:foo.object_id# => 2392...
Array joins<br />[1, 2, 3] * 3<br /># => [1, 2, 3, 1, 2, 3, 1, 2, 3]<br />[1, 2, 3] * '3'# => "13233" <br />%w{thisisatest...
Everything is an object!<br />defadd(adder, addee)<br />  adder +addee<br />end <br />add(3,5) # => 8<br />classFixnum<br ...
Use ranges instead of complex comparisons for numbers<br /># No more if x > 1000 && x < 2000 nonsense. Instead:<br />year ...
Ruby logic<br />defis_odd(x)  <br />  if x % 2==0<br />returnfalse<br />else<br />returntrue<br />end<br />end<br />defis_...
Enumerate single object<br /># [*items] converts a single object into an array. And if the object is an array, keeps it as...
14<br />Rails<br />
Caching<br />Page Caching<br />Fragment Caching<br />Action Caching<br />Caching into Local / Instance Variable<br />15<br...
Don’t limit yourself to ActiveRecord<br />Use performance boosting database <br />features like: <br />Stored procedures<b...
Finders are great but be careful<br />Retrieve only the information that you need. <br />Don’t kill your database with too...
Control your controllers<br />Don’t let them become the God classes.<br />Lesser instance variables.<br />Slimmer the bett...
19<br />And of course!<br />
20<br />Obey design principles.<br />Always code in context (Objects). It is the best way to model your solution.<br />Avo...
21<br />Slides<br />slideshare.net/ihower/rails-best-practices<br />slideshare.net/ihower/practical-rails2-350619<br />sli...
Prochain SlideShare
Chargement dans…5
×

Ruby Code Optimizations (for beginners)

2 798 vues

Publié le

A small presentation explaining a limited set of good practices and code optimization techniques in Ruby and Ruby on Rails for beginners.

Publié dans : Technologie, Formation
  • Soyez le premier à commenter

Ruby Code Optimizations (for beginners)

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

×