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 -- In reply to Re: sprintf vs. substr (for zero-padded values)
by thelenm
|
|