Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I found myself also asking this question based on some feedback I received from a recent question I posted - converting hex to char. In this string, unpack and printf were presented as options for converting data. To test the performance for each, I did the following:
#!c:/perl/bin/perl -w use strict; use POSIX qw(strftime); my $x; my $maxint = 200000; my $start = strftime "%H:%M:%S", localtime; for ($x=0; $x <$maxint;$x++) { print unpack "H*", "abc" } my $finish = strftime "%H:%M:%S", localtime; print "$start $finish";
Results: 01:32:57 01:33:48 (51 seconds)
#!c:/perl/bin/perl -w use strict; use POSIX qw(strftime); my $x; my $maxint = 200000; my $start = strftime "%H:%M:%S", localtime; for ($x=0; $x <$maxint;$x++) { printf "%x%x%x",ord('a'),ord('b'),ord('c'); } my $finish = strftime "%H:%M:%S", localtime; print "$start $finish";
Results: 01:31:56 01:32:50 (54 seconds)

In this case, unpack is the clear winner, although the performance difference doesn't become apparent until after 100000 iterations. So, in my opinion, being that TIMTOWTDI, I would look for a performance differential between these methods and opt for the one that requires the least amount of execution time.

The second thing I would check to see if any shelling out can be replaced by an available perl function. I recently wrote a program that required that the date/time stamps in a log file be updated. For this, I made the mistake of relying on shelling out

my $time1 = `date '+%H:%M:%S'`;
when I should have used

my $time1 = strftime "%H:%M:%S", localtime;
Hope this helps.

cheers, -semio


In reply to Re: Optimizing existing Perl code (in practise) by semio
in thread Optimizing existing Perl code (in practise) by JaWi

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 exploiting the Monastery: (8)
As of 2024-03-28 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found