<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Ruby Ternary operator re-written with Boolean operators</title>
	<atom:link href="http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/</link>
	<description>The Lightweight Ramblings of Jamie Hill</description>
	<lastBuildDate>Tue, 31 Jan 2012 20:06:51 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Jamie Hill</title>
		<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/comment-page-1/#comment-2150</link>
		<dc:creator>Jamie Hill</dc:creator>
		<pubDate>Fri, 24 Aug 2007 15:33:20 +0000</pubDate>
		<guid isPermaLink="false">http://8b9af4c0-38d3-444d-8072-94ec308fc3b2#comment-2150</guid>
		<description>raggi: Thanks, interesting stuff. I suppose it all comes down to boolean comparisons at the end of the day and the more operations needed the slower things will be (same with SQL queries).

Also speed depends on whether the value before the operator evaluates to true i.e. &lt;code&gt;true &#124;&#124; false&lt;/code&gt; would be faster than &lt;code&gt;false &#124;&#124; true&lt;/code&gt; as in the first example, the second value does not need checking but it does in the latter.

The inject for empty? is interesting, inject always makes my brain ache even though I&#039;ve been using it for years.</description>
		<content:encoded><![CDATA[<p>raggi: Thanks, interesting stuff. I suppose it all comes down to boolean comparisons at the end of the day and the more operations needed the slower things will be (same with SQL queries).</p>
<p>Also speed depends on whether the value before the operator evaluates to true i.e. <code>true || false</code> would be faster than <code>false || true</code> as in the first example, the second value does not need checking but it does in the latter.</p>
<p>The inject for empty? is interesting, inject always makes my brain ache even though I&#8217;ve been using it for years.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: raggi</title>
		<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/comment-page-1/#comment-2149</link>
		<dc:creator>raggi</dc:creator>
		<pubDate>Fri, 24 Aug 2007 05:56:22 +0000</pubDate>
		<guid isPermaLink="false">http://8b9af4c0-38d3-444d-8072-94ec308fc3b2#comment-2149</guid>
		<description>sidenote: the first example of the if/else block should have newlines, the latter should not.

Apologies for the complete lack of formatting.</description>
		<content:encoded><![CDATA[<p>sidenote: the first example of the if/else block should have newlines, the latter should not.</p>
<p>Apologies for the complete lack of formatting.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: raggi</title>
		<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/comment-page-1/#comment-2148</link>
		<dc:creator>raggi</dc:creator>
		<pubDate>Fri, 24 Aug 2007 02:57:13 +0000</pubDate>
		<guid isPermaLink="false">http://8b9af4c0-38d3-444d-8072-94ec308fc3b2#comment-2148</guid>
		<description>You&#039;re potentially performing two boolean operations too.

During benchmarking of a simple string building algorithm, I ended up looking at some of the more minor performance areas, like the boolean operators and functions.

Originally I &#039;discovered&#039; that if/else was faster than the ternary operator; however, further testing revealed this had more to do with specific testing code than anything else.

if true
true
else
false
end

is slower than

true ? true : false

However, their logic are the same.

Further testing (with the help of zenspider, probably more to his annoyance than anything else), I realised that:

if true then true else false end

is effectively the same speed as the ternary operator.

Interesting? Maybe.

Using &#039;return&#039; instead of implicit return values I also found to be slower.

On that note, you can also do this (which clearly isn&#039;t faster, it&#039;s just interesting) (from #ruby-lang):

# 02:38  flgr: interestingly, Array#empty? can be implemented as ary.inject(true) { false }
# 02:39  flgr: what this means is that you can replace  ary.empty?() ? a : b  with  ary.inject(a){ b }  all the time
# 02:50  flgr: raggi: you can probably make it faster by using { break b } :)

N.B. that break, like return, is slightly slower than not doing so, using the tests that I built some time ago, which may need to be re-done.

Analysis of speed at this level is extremely difficult from within the language, and is massively sensitive to environmental influence. It&#039;s almost easier to look at the source and rely on that.
</description>
		<content:encoded><![CDATA[<p>You&#8217;re potentially performing two boolean operations too.</p>
<p>During benchmarking of a simple string building algorithm, I ended up looking at some of the more minor performance areas, like the boolean operators and functions.</p>
<p>Originally I &#8216;discovered&#8217; that if/else was faster than the ternary operator; however, further testing revealed this had more to do with specific testing code than anything else.</p>
<p>if true<br />
true<br />
else<br />
false<br />
end</p>
<p>is slower than</p>
<p>true ? true : false</p>
<p>However, their logic are the same.</p>
<p>Further testing (with the help of zenspider, probably more to his annoyance than anything else), I realised that:</p>
<p>if true then true else false end</p>
<p>is effectively the same speed as the ternary operator.</p>
<p>Interesting? Maybe.</p>
<p>Using &#8216;return&#8217; instead of implicit return values I also found to be slower.</p>
<p>On that note, you can also do this (which clearly isn&#8217;t faster, it&#8217;s just interesting) (from #ruby-lang):</p>
<p># 02:38  flgr: interestingly, Array#empty? can be implemented as ary.inject(true) { false }<br />
# 02:39  flgr: what this means is that you can replace  ary.empty?() ? a : b  with  ary.inject(a){ b }  all the time<br />
# 02:50  flgr: raggi: you can probably make it faster by using { break b } <img src='http://thelucid.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>N.B. that break, like return, is slightly slower than not doing so, using the tests that I built some time ago, which may need to be re-done.</p>
<p>Analysis of speed at this level is extremely difficult from within the language, and is massively sensitive to environmental influence. It&#8217;s almost easier to look at the source and rely on that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie Hill</title>
		<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/comment-page-1/#comment-2147</link>
		<dc:creator>Jamie Hill</dc:creator>
		<pubDate>Thu, 23 Aug 2007 23:54:55 +0000</pubDate>
		<guid isPermaLink="false">http://8b9af4c0-38d3-444d-8072-94ec308fc3b2#comment-2147</guid>
		<description>Jonas: Very good point, I hadn&#039;t thought of that.</description>
		<content:encoded><![CDATA[<p>Jonas: Very good point, I hadn&#8217;t thought of that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jonas</title>
		<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/comment-page-1/#comment-2146</link>
		<dc:creator>Jonas</dc:creator>
		<pubDate>Thu, 23 Aug 2007 23:09:58 +0000</pubDate>
		<guid isPermaLink="false">http://8b9af4c0-38d3-444d-8072-94ec308fc3b2#comment-2146</guid>
		<description>That&#039;s a quite normal pattern in Python. There is one caveat to be aware of though, if you do &quot;foo = a and b or c&quot; you have to make sure b can&#039;t evaluate to false (because then the expression will return c).</description>
		<content:encoded><![CDATA[<p>That&#8217;s a quite normal pattern in Python. There is one caveat to be aware of though, if you do &#8220;foo = a and b or c&#8221; you have to make sure b can&#8217;t evaluate to false (because then the expression will return c).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jamie Hill</title>
		<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/comment-page-1/#comment-2145</link>
		<dc:creator>Jamie Hill</dc:creator>
		<pubDate>Thu, 23 Aug 2007 20:14:39 +0000</pubDate>
		<guid isPermaLink="false">http://8b9af4c0-38d3-444d-8072-94ec308fc3b2#comment-2145</guid>
		<description>Rik: Ohhh... touchy! It was only a simple observation.

I wasn&#039;t advocating a complete paradigm shift. Just wondered if anyone had come across it before and had noticed any performance gains.</description>
		<content:encoded><![CDATA[<p>Rik: Ohhh&#8230; touchy! It was only a simple observation.</p>
<p>I wasn&#8217;t advocating a complete paradigm shift. Just wondered if anyone had come across it before and had noticed any performance gains.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rik Hemsley</title>
		<link>http://thelucid.com/2007/08/23/ruby-ternary-operator-re-written-with-boolean-operators/comment-page-1/#comment-2144</link>
		<dc:creator>Rik Hemsley</dc:creator>
		<pubDate>Thu, 23 Aug 2007 14:40:08 +0000</pubDate>
		<guid isPermaLink="false">http://8b9af4c0-38d3-444d-8072-94ec308fc3b2#comment-2144</guid>
		<description>&quot;I don&#039;t know if there is any performance gain here, anyone care to investigate?&quot;

Not really. I&#039;m not in the habit of obfuscating my code to take advantage of minor (it can&#039;t be anything but) performance gains. Also, who&#039;s to say the interpreter won&#039;t change to make the alternate method faster in the future?
</description>
		<content:encoded><![CDATA[<p>&#8220;I don&#8217;t know if there is any performance gain here, anyone care to investigate?&#8221;</p>
<p>Not really. I&#8217;m not in the habit of obfuscating my code to take advantage of minor (it can&#8217;t be anything but) performance gains. Also, who&#8217;s to say the interpreter won&#8217;t change to make the alternate method faster in the future?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

