Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Well here it is: Proof that you can really perform a task in multiple ways. I needed an efficient way to cut off trailing whitespace. I was pulling values from a database and the scalars all had trailing whitespace. I had mistakenly made the column type CHAR(10) when it should have been a VARCHAR. Let me just say that the details of the job make it alright to cut the whitespace instead of fixing the underlying problem

So I had three ideas (none original) to perform this task. Here is my benchmark script.

use Benchmark; timethese (1000000, { 'unpack' => q{ my $foo = "test "; $foo = unpack("A8",$foo); }, 'regex' => q{ my $foo = "test "; $foo =~ s/\s+$//;}, 'xeger' => q{ my $foo = reverse "test "; $foo =~ s/^\s+//; $foo = reverse $foo;} } );

The standard regex, the unpack function, and the reverse regex. I figured the reverse one maybe wouldn't apply because the is no .* in the match. But it is cool enough an idea to try out.

Here are my results:

Benchmark: timing 1000000 iterations of regex, unpack, xeger... regex: 6 wallclock secs ( 5.71 usr + 0.00 sys = 5.71 CPU) @ 17 +5131.35/s (n=1000000) unpack: 7 wallclock secs ( 5.91 usr + 0.00 sys = 5.91 CPU) @ 16 +9204.74/s (n=1000000) xeger: 9 wallclock secs ( 9.26 usr + 0.00 sys = 9.26 CPU) @ 10 +7991.36/s (n=1000000)

So it seems that the regex and the unpack are very close to the same time. Notice that this is for 1 million iterations. All methods are fast. So you can use various methods for this task!

Thank you, goodnight.

In reply to Removing Trailing Whitespace: Multiple ways. by DeaconBlues

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-18 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found