String Theory
The other day I found myself in an argument about whether or not a single quoted string was faster than a double quoted string. I know that there are conventions that dictate the use of one over the other for clarity, but I was curious to see if there was actually a performance component. I being on the side of the single quoted string(SQ) thought surely since SQ strings aren't interpolated they'd be loads faster. Unfortunately, proving myself right was not as easy as I had initially thought.
Define the parameters
The first thing I needed to do to prove my point was to define how we would measure and quantify success of one string over the other. We came up with these criteria:
The positive difference must be statistically significant.
The test must be consistent.
The sample size must be ample enough to account for any external factors.
The results
Right out of the shoots you can see that there is actually very little difference. I found that single quoted strings were about ~0.19% faster than double quoted strings. This is hardly the victory I was hoping for. The difference was more pronounced in jruby, but the test was significantly more erratic.
Anyways, I won right? Wrong! Let's move onto the methodology and dig into why it might still be too early to bust out the ticker tape.
Methodology
```sh
Page not found · GitHub
<script type="text/javascript">
(function(e){function l(){return f==true?false:window.DeviceOrientationEvent!=undefined}function c(e){x=e.gamma;y=e.beta;if(Math.abs(window.orientation)===90){var t=x;x=y;y=t}if(window.orientation<0){x=-x;y=-y}u=u==null?x:u;a=a==null?y:a;return{x:x-u,y:y-a}}function h(e){if((new Date).getTime()<r+n)return;r=(new Date).getTime();var t=s.offset()!=null?s.offset().left:0,u=s.offset()!=null?s.offset().top:0,a=e.pageX-t,h=e.pageY-u;if(a<0||a>s.width()||h<0||h>s.height())return;if(l()){if(e.gamma==undefined){f=true;return}values=c(e);a=values.x/30;h=values.y/30}var p=a/(l()==true?o:s.width()),d=h/(l()==true?o:s.height()),v,m;for(m=i.length;m--;){v=i[m];newX=v.startX+v.inversionFactor*v.xRange*p;newY=v.startY+v.inversionFactor*v.yRange*d;if(v.background){v.obj.css("background-position",newX+"px "+newY+"px")}else{v.obj.css("left",newX).css("top",newY)}}}var t=25,n=1/t*1e3,r=(new Date).getTime(),i=[],s=e(window),o=1,u=null,a=null,f=false;e.fn.plaxify=function(t){return this.each(function(){var n=-1;var r={xRange:e(this).data("xrange")||0,yRange:e(this).data("yrange")||0,invert:e(this).data("invert")||false,background:e(this).data("background")||false};for(var s=0;s<i.length;s++){if(this===i[s].obj.get(0)){n=s}}for(var o in t){if(r[o]==0){r[o]=t[o]}}r.inversionFactor=r.invert?-1:1;r.obj=e(this);if(r.background){pos=(r.obj.css("background-position")||"0px 0px").split(/ /);if(pos.length!=2){return}x=pos[0].match(/^((-?\d+)\s*px|0+\s*%|left)$/);y=pos[1].match(/^((-?\d+)\s*px|0+\s*%|top)$/);if(!x||!y){return}r.startX=x[2]||0;r.startY=y[2]||0}else{var u=r.obj.position();r.obj.css({top:u.top,left:u.left,right:"",bottom:""});r.startX=this.offsetLeft;r.startY=this.offsetTop}r.startX-=r.inversionFactor*Math.floor(r.xRange/2);r.startY-=r.inversionFactor*Math.floor(r.yRange/2);if(n>=0){i.splice(n,1,r)}else{i.push(r)}})};e.plax={enable:function(t){e(document).bind("mousemove.plax",function(n){if(t){s=t.activityTarget||e(window)}h(n)});if(l()){window.ondeviceorientation=function(e){h(e)}}},disable:function(t){e(document).unbind("mousemove.plax");window.ondeviceorientation=undefined;if(t&&typeof t.clearLayers==="boolean"&&t.clearLayers)i=[]}};if(typeof ender!=="undefined"){e.ender(e.fn,true)}})(function(){return typeof jQuery!=="undefined"?jQuery:ender}())
</script>
<script type="text/javascript">
// Plaxify all `js-plaxify` element layers
var layers = $('.js-plaxify')
$.each(layers, function(index, layer){
$(layer).plaxify({
xRange: $(layer).data('xrange') || 0,
yRange: $(layer).data('yrange') || 0,
invert: $(layer).data('invert') || false
})
})
$.plax.enable()
$.ajax({
url: '/sessions/login_404?return_to='+window.location.pathname,
success: function(data) {
if (data != ' ') {
$('#auth').html(data).slideDown(100)
$('#login_field').attr("placeholder", "Username or Email")
$('#password').attr("placeholder", "Password")
}
}
});
$(document).on('keydown', function(event) {
if (event.target === document.body && event.keyCode === 192 && !event.metaKey) {
$('#parallax_wrapper').css('-webkit-filter','grayscale(25%)')
document.cookie = 'stats=yes; path=/';
setTimeout(function() {
location.reload();
}, 250)
return false;
}
})
</script>
</noscript>
In the above code I'm requiring './stats.rb' which is just a down and dirty stats class that I created [[1]](https://gist.github.com/1346872). All it does is let me calculate standard deviation and export the results to a csv file. So here I ran each test string a thousand times calculating and storing the result(seconds) for each. The test string simply instantiates a hundred thousand _a_'s and returns the time it took. Sure enough we get that ~0.19% increase. Nothing too fancy here.
Not so fast.
Remember that we specified criteria for this argument. The test is mostly consistent so we satisfy rule 2. Rule 3 also as 1000 results is probably an ample enough size. However not so lucky when we run into rule 1. The standard deviation for the single quoted string was larger than the SQ string speed increase(at ~.003s). This discrepancy means, at least to me, that the difference in speed between a sinqle quoted string and double quoted string is not statistically significant.
If I had more time I would increase the result set and attempt to account for outliers, but I think that this is probably a fairly typical outcome.
## Conclusions
At first I thought that this entire endeavor was a waste. But, I think this was actually quite good for me. It made me think scientifically and outside the box. Also, the pestering question about whether it's better to use a single quoted string vs a double quoted string can once again be relegated to a semantic/style argument and not a practical one. Anyways I had fun writing this one for sure, hope you found it useful.
If you like this sort of thing you should subscribe to this blog, thanks for reading.
#### References
<span style="font-size:12px;">1.) [Down and Dirty Stats](https://gist.github.com/1346872)</span>
#### Additional References
<span style="font-size:12px;">[XKCD 171](http://xkcd.com/171/)</span>
Please enable JavaScript to view the comments powered by Disqus.
blog comments powered by