
Writing Efficient Ruby Code (Digital Short Cut)
Description
Alles über E-Books | Antworten auf Fragen rund um E-Books, Kopierschutz und Dateiformate finden Sie in unserem Info- & Hilfebereich.
This short cut focuses on a number of coding patterns that are useful when trying to get maximum speed out of performance-critical sections of Ruby code. Anti-patterns, that is, coding idioms, which should be avoided in performance-sensitive code sections are discussed, including details on how to transform such code to make it more efficient. Most patterns were extracted from Stefan Kaes' work on improving performance of the Rails core and his regular Rails performance consulting work. These patterns are largely non-algorithmic, detailing local code transformations to achieve identical results with slightly different and faster code, as even local code changes can sometimes result in orders of magnitude improvements. Some patterns are useful independent of Rails' implementation language, but some of them are specific to Ruby, or more specifically, the current implementation of Ruby. Converts from other languages, especially from statically typed languages such as Java or C++ may find this material useful, as the performance characteristics of certain operations, like performing a function call or accessing object fields/attributes, are quite different from what you expect.
What This Short Cut Covers
Introduction
Ruby's Interpreter Is Slow
Runtime Complexity of Ruby Language Constructs
Patterns
Instance Variables versus Accessors
Local Variables Are Cheap
Assignments in Expressions
Interpolated Strings
In-Place Updates
Sets versus Arrays
For Loops versus each
Make Decisions at Load Time
Self Modifying Code
Test Most Frequent Case First
Optimize Access to Global Constants
Caching Data in Instance Variables
Caching Data in Class Variables
Coding Variable Caching Efficiently
Initializing Variables with nil
Using .nil?
nil? or empty? versus blank?
Using return
Using returning
Using any?
Block Local Variables
Date Formatting
Temporary Datastructure Constants
File System Access
ObjectSpace.each_object
Unnecessary Block Parameters
Symbol.to_proc
Chained Calls of map
Requiring Files Dynamically
Including Modules versus Opening Classes
About the Author
More details
Person
Dr. Stefan Kaes led Rails' core team effort to achieve acceptable performance for the 1.0 release. Stefan is a software consultant in Germany and previously taught programming at the Technical University of Darmstadt. He regularly contributes patches to Rails core and has a successful Rails performance blog, http://railsexpress.de/blog/. In January 2005 he discovered Rails by accident, learned Ruby in about a week, and became convinced this is the right way to develop web applications due to the simplicity, flexibility, and power achieved by the combination of the Rails design and Ruby's language characteristics.
Content
What This Short Cut Covers
Introduction
Ruby's Interpreter Is Slow
Runtime Complexity of Ruby Language Constructs
Patterns
Instance Variables versus Accessors
Local Variables Are Cheap
Assignments in Expressions
Interpolated Strings
In-Place Updates
Sets versus Arrays
For Loops versus each
Make Decisions at Load Time
Self Modifying Code
Test Most Frequent Case First
Optimize Access to Global Constants
Caching Data in Instance Variables
Caching Data in Class Variables
Coding Variable Caching Efficiently
Initializing Variables with nil
Using .nil?
nil? or empty? versus blank?
Using return
Using returning
Using any?
Block Local Variables
Date Formatting
Temporary Datastructure Constants
File System Access
ObjectSpace.each_object
Unnecessary Block Parameters
Symbol.to_proc
Chained Calls of map
Requiring Files Dynamically
Including Modules versus Opening Classes
About the Author
System requirements
File format: PDF
Copy protection: Watermark-DRM (Digital Rights Management)
System requirements:
- Computer (Windows; MacOS X; Linux): Use the free software Adobe Reader, Adobe Digital Editions, or any other PDF viewer of your choice (see eBook Help).
- Tablet/Smartphone (Android; iOS): Install the free app Adobe Digital Editions or another reading app for eBooks, e.g., PocketBook (see eBook Help).
- E-reader: Bookeen, Kobo, Pocketbook, Sony, Tolino and many more (only limited: Kindle).
The file format PDF always displays a book page identically on any hardware. This makes PDF suitable for complex layouts such as those used in textbooks and reference books (images, tables, columns, footnotes). Unfortunately, on the small screens of e-readers or smartphones, PDFs are rather annoying, requiring too much scrolling.
This eBook uses Watermark-DRM, a „soft” copy protection. This means that there are no technical restrictions to prevent illegal distribution. However, there is a personalised watermark embedded in the eBook that can be used to identify the purchaser of the eBook in the event of misuse and to provide evidence for legal purposes.
For more information, see our eBook Help page.