Hi Monks,

I have written a (working) perl program, but it is quite slow. I have troubles to find out why. Is there a better way to find out why, than guess and try ?? How do you start a speed analyses to find out why a program is slow ?

Thanks for your help !!!

My program reads a complete binary file into $f (Not show in the code fragment). Than it decodes following structure:

The first byte is the first data byte. (range from 0 to 59, 255 means datastream is over)

The second byte defines how many "position bytes" bytes are following. (binary: 1=position byte follows, so 1 to max 8 position bytes, 0=position byte equal to 0x00 skipped)

The position bytes define which data bytes are changed in this data set. (binary: 1=data byte changed, so max 64 data bytes)

Now the databytes follows (one for each binary 1 in a position byte), followed by a CRC byte of this dataset.

Than the next data set is comming, until 255 is following.

The program checks the data and does statistics. (Not shown in my code fragment here. Also when this part is commented out, the program is too slow !,)

Hope this description was not too short for this data structure.

Please find my program here:

$crc=255; $data[0]=readByte(); do { $_=sprintf("%08b",readByte()); + # BYTE1 $byte2=''; while (m/(.)/g) { if ($1 == 0) { $byte2.='00000000';} else { $byte2.=sprintf("%08b",readByte());} + # positions bytes } $data[pos($byte2)]=readByte() while ($byte2=~m/(1)/g); + # put data value into @data on the correct position readByte(); if ($crc>0) {print "ERROR: CRC (POS:".pos($f).')'; ex +it; } # crc check $crc=255; # ---- USE DATA => DELETED -------- $data[0]=readByte(); } while ($data[0]<255); } sub readByte { unless ($f=~m/(.)/gs) {print "Last data structure not complete !"; e +xit;} my $x=ord($1); $crc^=$x; # CRC for my $r(1..8) { if (($crc&1)>0) {$crc=($crc>>1)^0x8c;} else {$crc=$crc>>1;} } return ($x); }

In reply to How to find out, why my perl code is slow. by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.