Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
For me, anyway, the goal is not always the absolute fastest performance possible (if it were, maybe I would write more programs in C). In this case, all else being equal, I would probably use sprintf over substr. I think that's mostly because the code is easier for me to understand at a glance. When I come back to this snippet of code a few months from now, it will take me longer to understand what the substr is doing.

Also, sprintf has functionality specifically intended for what we're using it for. It seems like we have to play with substr a little bit to get it to do what we need it to. If we have to use this code more than once, we'll either have to remember how we massaged substr and do it the exact same way again, or else put the substr solution in a subroutine of its own. Which is fine, but a little more effort.

To me anyway, the substr hoops are a little too large for me to jump through for a mere 2-3% gain in performance. But it could easily be that there are performance-critical applications for which the substr solution would be appropriate.

-- Mike

--
just,my${.02}


In reply to Re: sprintf vs. substr (for zero-padded values) by thelenm
in thread sprintf vs. substr (for zero-padded values) by Theseus

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 browsing the Monastery: (5)
As of 2024-03-28 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found