Habits die hard... - No Fluff Just Stuff

Habits die hard...

Posted by: Venkat Subramaniam on July 14, 2005

 I used "" instead of '' in my example for yield. A visitor commented that a single quote is better than double quote.

"Not to be picky either, but does the above ruby code need single quotes?
Around 'Calling GetValue2' and puts ahead of the call to GetValue2?"

That is correct, but I have years of C++, Java, C# experience to undo! :)

What's in double quotes is evaluated in Ruby. However, what's in single quote is not. If you don't have anything to
expand, use single quote.

I said to myself, "wonder what the price I pay for using double quotes?" When I got into C++, Java, C#, it took me
a while to measure the time and generate a report. Ruby, as sweet it is, comes with a nice benchmark library. So, I
decided to use that to find out what is the penalty for double quotes usage. Here is what I wrote:

require 'benchmark'
include Benchmark

def dummy(str)
    str.length
end

TIMES_TO_RUN = 10000
x = 1
bm(10) do |timeIt|
    timeIt.report("double:") { TIMES_TO_RUN.times { dummy("test#{x}") }}
    timeIt.report("single:") { TIMES_TO_RUN.times { dummy('test#{x}') }}
end

The dummy method returns the length of the parameter. I call dummy 10K times.
(I could not find any time difference when I called it a 1000 times, so I bumped it to 10000).
I got the following output on my machine when I ran the above code (time in seconds):

                      user     system         total            real
double:     0.030000   0.000000   0.030000 (  0.030000)
single:     0.010000   0.000000   0.010000 (  0.010000)

User CPU time, System CPU time, total of the two, and real time elapsed.

Venkat Subramaniam

About Venkat Subramaniam

Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., creator of agilelearner.com, and an instructional professor at the University of Houston.

He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with sustainable agile practices on their software projects.

Venkat is a (co)author of multiple technical books, including the 2007 Jolt Productivity award winning book Practices of an Agile Developer. You can find a list of his books at agiledeveloper.com. You can reach him by email at venkats@agiledeveloper.com or on twitter at @venkat_s.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »