Please find the (simplified) program below. The file is simulated with an array. It is running itself.

#!/usr/bin/perl use strict; use warnings; $\="\n"; $,=", "; #------------------------------------------------ my($f, $fpos, $fposlen, @data, $byte1, @byte2, $crc, @crcval); initCRC(); ReadDatFile(); exit; #------------------------------------------------ sub ReadDatFile { # simulate file read: # three datasets: 0,1,2,3,4,5,6,7,8,9 and 10,11,2,3,4,5,6,7,8,9 a +nd 20,11,2,3,4,5,6,7,18,19 my @file=(0x00, 0xc0,0xff,0x80,1,2,3,4,5,6,7,8,9,0x8e, 10,0x80,0x8 +0,11,0x9d, 20,0xc0,0x01,0x80,18,19,0x1c, 0xff); $f.=chr($_)|'' foreach (@file); $crc=255; $fposlen=length($f); $fpos=0; @byte2=(0,0,0,0,0,0,0,0); readByte(); $data[0]=$_; # first data do { readByte(); $byte1=$_; # BYTE1: $_ if ($byte1 & 0x80) {readByte(); $byte2[0]=$_; } if ($byte1 & 0x40) {readByte(); $byte2[1]=$_; } if ($byte1 & 0x20) {readByte(); $byte2[2]=$_; } if ($byte1 & 0x10) {readByte(); $byte2[3]=$_; } if ($byte1 & 0x08) {readByte(); $byte2[4]=$_; } if ($byte1 & 0x04) {readByte(); $byte2[5]=$_; } if ($byte1 & 0x02) {readByte(); $byte2[6]=$_; } if ($byte1 & 0x01) {readByte(); $byte2[7]=$_; } do_byte2( 0) if ($byte1 & 0x80); do_byte2( 8) if ($byte1 & 0x40); do_byte2(16) if ($byte1 & 0x20); do_byte2(24) if ($byte1 & 0x10); do_byte2(32) if ($byte1 & 0x08); do_byte2(40) if ($byte1 & 0x04); do_byte2(48) if ($byte1 & 0x02); do_byte2(56) if ($byte1 & 0x01); readByte(); if ($crc>0) {print "ERROR: CRC (POS:$fpos)"; exit; } + # crc check $crc=255; print @data; # ---- USE DATA --------- readByte(); $data[0]=$_; # next dataset } while ($_<255); } sub do_byte2 { my $bpos=$_[0]; # pos in data fr +om byte0 my $pp=$byte2[$bpos>>3]; if ($pp & 0x80) {readByte(); $data[$bpos+1]=$_;} # take value int +o data array if ($pp & 0x40) {readByte(); $data[$bpos+2]=$_;} if ($pp & 0x20) {readByte(); $data[$bpos+3]=$_;} if ($pp & 0x10) {readByte(); $data[$bpos+4]=$_;} if ($pp & 0x08) {readByte(); $data[$bpos+5]=$_;} if ($pp & 0x04) {readByte(); $data[$bpos+6]=$_;} if ($pp & 0x02) {readByte(); $data[$bpos+7]=$_;} if ($pp & 0x01) {readByte(); $data[$bpos+8]=$_;} } sub readByte { if ($fposlen==$fpos) {print "Last data structure not complete !"; ex +it;} $_=vec($f,$fpos,8); $crc=$crcval[$crc^$_]; # CRC $fpos++; } sub initCRC { for my $v (0..255) { $_=$v; for my $r (1..8) { if (($_&1)>0) {$_=($_>>1)^0x8c;} else {$_=$_>>1;} } $crcval[$v]=$_; } }

In reply to Re^4: How to find out, why my perl code is slow. by Anonymous Monk
in thread 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.