<?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 for Laufeyjarson writes...</title>
	<atom:link href="http://blog.laufeyjarson.com/comments/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.laufeyjarson.com</link>
	<description>... notes, thoughts, rants, and randomness.</description>
	<pubDate>Tue, 09 Mar 2010 22:43:18 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by John Wiersba</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-5272</link>
		<dc:creator>John Wiersba</dc:creator>
		<pubDate>Fri, 05 Mar 2010 20:49:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-5272</guid>
		<description>I think the hands down winner is $str =~ s/^\s+//; $str =~ s/\s+\z//; 

It says exactly what you want and it's the fastest.  No need to "optimize" with a single regex.  Although dami's postfix-for form is 7 characters shorter, the need for a comma instead of semicolon is a blemish, plus it's about 25% slower.

#!/usr/bin/perl -w
use strict;
use Benchmark;
use vars qw($str1);
$str1 = "        asf asd fasd fasd f asdf asd asdf    as fsda fasdf        ";
timethese( -5, {
   test1_0 =&gt; ' $str = $str1; $str =~ s/^\s+&#124;\s+\z//g ',
   test1_1 =&gt; ' $str = $str1; $str =~ s/^\s*&#124;\s*\z//g ',
   test1_2 =&gt; ' $str = $str1; $str =~ s/^\s+&#124;\s+$//g ',
   test1_3 =&gt; ' $str = $str1; $str =~ s/^\s*&#124;\s*$//g ',
   test2_0 =&gt; ' $str = $str1; $str =~ s/^\s+//; $str =~ s/\s+\z// ',
   test2_1 =&gt; ' $str = $str1; $str =~ s/^\s+//; $str =~ s/\s+$// ',
   test2_2 =&gt; ' $str = $str1; $str =~ s/^\s*//; $str =~ s/\s*\z// ',
   test2_3 =&gt; ' $str = $str1; $str =~ s/^\s*//; $str =~ s/\s*$// ',
   test3   =&gt; ' $str = $str1; s/^\s+//, s/\s+\z// for $str ',
});

Benchmark: running test1_0, test1_1, test1_2, test1_3, test2_0, test2_1, test2_2, test2_3, test3 for at least 5 CPU seconds...
   test1_0:  6 wallclock secs ( 5.29 usr +  0.00 sys =  5.29 CPU) @ 185981.10/s (n=983840)
   test1_1:  5 wallclock secs ( 5.22 usr +  0.00 sys =  5.22 CPU) @ 154480.84/s (n=806390)
   test1_2:  6 wallclock secs ( 5.22 usr +  0.00 sys =  5.22 CPU) @ 188475.10/s (n=983840)
   test1_3:  5 wallclock secs ( 5.25 usr +  0.00 sys =  5.25 CPU) @ 154564.00/s (n=811461)
   test2_0:  5 wallclock secs ( 5.26 usr +  0.01 sys =  5.27 CPU) @ 672072.87/s (n=3541824)
   test2_1:  6 wallclock secs ( 5.44 usr +  0.00 sys =  5.44 CPU) @ 651070.77/s (n=3541825)
   test2_2:  5 wallclock secs ( 5.17 usr +  0.00 sys =  5.17 CPU) @ 248851.26/s (n=1286561)
   test2_3:  5 wallclock secs ( 5.23 usr +  0.00 sys =  5.23 CPU) @ 241523.71/s (n=1263169)
     test3:  4 wallclock secs ( 5.29 usr +  0.00 sys =  5.29 CPU) @ 526948.02/s (n=2787555)</description>
		<content:encoded><![CDATA[<p>I think the hands down winner is $str =~ s/^\s+//; $str =~ s/\s+\z//; </p>
<p>It says exactly what you want and it&#8217;s the fastest.  No need to &#8220;optimize&#8221; with a single regex.  Although dami&#8217;s postfix-for form is 7 characters shorter, the need for a comma instead of semicolon is a blemish, plus it&#8217;s about 25% slower.</p>
<p>#!/usr/bin/perl -w<br />
use strict;<br />
use Benchmark;<br />
use vars qw($str1);<br />
$str1 = &#8221;        asf asd fasd fasd f asdf asd asdf    as fsda fasdf        &#8220;;<br />
timethese( -5, {<br />
   test1_0 =&gt; &#8216; $str = $str1; $str =~ s/^\s+|\s+\z//g &#8216;,<br />
   test1_1 =&gt; &#8216; $str = $str1; $str =~ s/^\s*|\s*\z//g &#8216;,<br />
   test1_2 =&gt; &#8216; $str = $str1; $str =~ s/^\s+|\s+$//g &#8216;,<br />
   test1_3 =&gt; &#8216; $str = $str1; $str =~ s/^\s*|\s*$//g &#8216;,<br />
   test2_0 =&gt; &#8216; $str = $str1; $str =~ s/^\s+//; $str =~ s/\s+\z// &#8216;,<br />
   test2_1 =&gt; &#8216; $str = $str1; $str =~ s/^\s+//; $str =~ s/\s+$// &#8216;,<br />
   test2_2 =&gt; &#8216; $str = $str1; $str =~ s/^\s*//; $str =~ s/\s*\z// &#8216;,<br />
   test2_3 =&gt; &#8216; $str = $str1; $str =~ s/^\s*//; $str =~ s/\s*$// &#8216;,<br />
   test3   =&gt; &#8216; $str = $str1; s/^\s+//, s/\s+\z// for $str &#8216;,<br />
});</p>
<p>Benchmark: running test1_0, test1_1, test1_2, test1_3, test2_0, test2_1, test2_2, test2_3, test3 for at least 5 CPU seconds&#8230;<br />
   test1_0:  6 wallclock secs ( 5.29 usr +  0.00 sys =  5.29 CPU) @ 185981.10/s (n=983840)<br />
   test1_1:  5 wallclock secs ( 5.22 usr +  0.00 sys =  5.22 CPU) @ 154480.84/s (n=806390)<br />
   test1_2:  6 wallclock secs ( 5.22 usr +  0.00 sys =  5.22 CPU) @ 188475.10/s (n=983840)<br />
   test1_3:  5 wallclock secs ( 5.25 usr +  0.00 sys =  5.25 CPU) @ 154564.00/s (n=811461)<br />
   test2_0:  5 wallclock secs ( 5.26 usr +  0.01 sys =  5.27 CPU) @ 672072.87/s (n=3541824)<br />
   test2_1:  6 wallclock secs ( 5.44 usr +  0.00 sys =  5.44 CPU) @ 651070.77/s (n=3541825)<br />
   test2_2:  5 wallclock secs ( 5.17 usr +  0.00 sys =  5.17 CPU) @ 248851.26/s (n=1286561)<br />
   test2_3:  5 wallclock secs ( 5.23 usr +  0.00 sys =  5.23 CPU) @ 241523.71/s (n=1263169)<br />
     test3:  4 wallclock secs ( 5.29 usr +  0.00 sys =  5.29 CPU) @ 526948.02/s (n=2787555)</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by dami</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4796</link>
		<dc:creator>dami</dc:creator>
		<pubDate>Tue, 02 Mar 2010 19:58:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4796</guid>
		<description>Jeffrey Friedl recommends using 2 regexes. But you can do it in one line :

s/^\s+//, s/\s+$// for $str;</description>
		<content:encoded><![CDATA[<p>Jeffrey Friedl recommends using 2 regexes. But you can do it in one line :</p>
<p>s/^\s+//, s/\s+$// for $str;</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by mattp</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4709</link>
		<dc:creator>mattp</dc:creator>
		<pubDate>Tue, 02 Mar 2010 14:40:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4709</guid>
		<description>its slower due to the backtracking required from the combination of two greedy quantifiers with the one non greedy. 
to quote knuth .. premature optimization is the root of all evil</description>
		<content:encoded><![CDATA[<p>its slower due to the backtracking required from the combination of two greedy quantifiers with the one non greedy.<br />
to quote knuth .. premature optimization is the root of all evil</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by Hercynium</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4561</link>
		<dc:creator>Hercynium</dc:creator>
		<pubDate>Tue, 02 Mar 2010 05:34:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4561</guid>
		<description>What about: 

$str =~ s/^\s*&#124;\s*$//g

Seems clearer and will probably be faster.</description>
		<content:encoded><![CDATA[<p>What about: </p>
<p>$str =~ s/^\s*|\s*$//g</p>
<p>Seems clearer and will probably be faster.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by Dave Rolsky</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4543</link>
		<dc:creator>Dave Rolsky</dc:creator>
		<pubDate>Tue, 02 Mar 2010 04:05:34 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4543</guid>
		<description>Yes, it's slower, but look at the absolute speed. The "slow" version can still execute 440,000 or so per minute!

Write code as clearly as possible, _then_ optimize the slow parts. This is what profilers are for.

This sort of premature micro-optimization makes for messy code that may actually be no faster than clean code.

Also, I think the simplest way to write it is ...

$str =~ s/^\s+&#124;\s+$//g;</description>
		<content:encoded><![CDATA[<p>Yes, it&#8217;s slower, but look at the absolute speed. The &#8220;slow&#8221; version can still execute 440,000 or so per minute!</p>
<p>Write code as clearly as possible, _then_ optimize the slow parts. This is what profilers are for.</p>
<p>This sort of premature micro-optimization makes for messy code that may actually be no faster than clean code.</p>
<p>Also, I think the simplest way to write it is &#8230;</p>
<p>$str =~ s/^\s+|\s+$//g;</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by Yanick</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4535</link>
		<dc:creator>Yanick</dc:creator>
		<pubDate>Tue, 02 Mar 2010 03:50:28 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4535</guid>
		<description>What about 

$str =~ s/^\s+&#124;\s+$//g

?

It's faster, and not too obtuse.</description>
		<content:encoded><![CDATA[<p>What about </p>
<p>$str =~ s/^\s+|\s+$//g</p>
<p>?</p>
<p>It&#8217;s faster, and not too obtuse.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by Yanick</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4533</link>
		<dc:creator>Yanick</dc:creator>
		<pubDate>Tue, 02 Mar 2010 03:49:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4533</guid>
		<description>What about 

$str =~ s/^\s+&#124;\s+$//g

?</description>
		<content:encoded><![CDATA[<p>What about </p>
<p>$str =~ s/^\s+|\s+$//g</p>
<p>?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by Mark Grimes</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4529</link>
		<dc:creator>Mark Grimes</dc:creator>
		<pubDate>Tue, 02 Mar 2010 03:32:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4529</guid>
		<description>What about:

$str =~ s/^\s*&#124;\s*$//g;

I can't test or benchmark it right now, but I know I've used something along these lines before.</description>
		<content:encoded><![CDATA[<p>What about:</p>
<p>$str =~ s/^\s*|\s*$//g;</p>
<p>I can&#8217;t test or benchmark it right now, but I know I&#8217;ve used something along these lines before.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by zloyrusskiy</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4528</link>
		<dc:creator>zloyrusskiy</dc:creator>
		<pubDate>Tue, 02 Mar 2010 03:31:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4528</guid>
		<description>I'm using:

s/(?:^\s+&#124;\s+$)//g

it's one regular expression + has good speed</description>
		<content:encoded><![CDATA[<p>I&#8217;m using:</p>
<p>s/(?:^\s+|\s+$)//g</p>
<p>it&#8217;s one regular expression + has good speed</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comment on Stripping whitespace from both ends of a string&#8230; by Adam Kennedy</title>
		<link>http://blog.laufeyjarson.com/2010/03/stripping-whitespace-from-both-ends-of-a-string/comment-page-1/#comment-4522</link>
		<dc:creator>Adam Kennedy</dc:creator>
		<pubDate>Tue, 02 Mar 2010 03:19:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.laufeyjarson.com/?p=90#comment-4522</guid>
		<description>$str =~ s/(?:^\s+&#124;\s+$)//g;</description>
		<content:encoded><![CDATA[<p>$str =~ s/(?:^\s+|\s+$)//g;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
