Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Interesting.... and its nearly as fast as inlining it in C.
#!/usr/bin/perl -w use strict; use Benchmark 'cmpthese'; my $str = "some string here"; die unless (pop_chord($str) . inlined($str) . _substr($str) . _unpack($str) . _match($str) eq substr($str,0,1)x5); cmpthese( 500000, { 'pop_chord' => sub { pop_chord($str) }, 'inlined' => sub { inlined($str) }, '_substr' => sub { _substr($str) }, '_unpack' => sub { _unpack($str) }, '_match' => sub { _match($str) }, } ); sub pop_chord { chr ord pop } sub _substr { substr pop,0,1 } sub _unpack { unpack"a",pop } sub _match { (pop=~/(.)/s)[0] } use Inline C => <<'END_OF_C_CODE'; char inlined(char* str) { return str[0]; } END_OF_C_CODE __END__ Benchmark: timing 500000 iterations of _match, _substr, _unpack, inl +ined, pop_chord... _match: 3 wallclock secs ( 3.98 usr + 0.00 sys = 3.98 CPU) @ 12 +5628.14/s (n=500000) _substr: 2 wallclock secs ( 1.45 usr + 0.00 sys = 1.45 CPU) @ 34 +4827.59/s (n=500000) _unpack: 4 wallclock secs ( 2.68 usr + 0.00 sys = 2.68 CPU) @ 18 +6567.16/s (n=500000) inlined: 1 wallclock secs ( 0.58 usr + 0.00 sys = 0.58 CPU) @ 86 +2068.97/s (n=500000) pop_chord: 1 wallclock secs ( 1.30 usr + 0.00 sys = 1.30 CPU) @ 38 +4615.38/s (n=500000) Rate _match _unpack _substr pop_chord inlined _match 125628/s -- -33% -64% -67% -85% _unpack 186567/s 49% -- -46% -51% -78% _substr 344828/s 174% 85% -- -10% -60% pop_chord 384615/s 206% 106% 12% -- -55% inlined 862069/s 586% 362% 150% 124% --

-Blake


In reply to Re: Read the first character of a string by blakem
in thread Read the first character of a string by jmcnamara

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 perusing the Monastery: (6)
As of 2024-04-19 10:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found