Looking at your code, I see several problems that may be slowing it down. First of all using the . to merge two strings does slow down things a lot! Secondly, you use sprintf to convert a byte to binary format, which is unnecessary, I think. You could go around that and just perform the necessary operations without converting the number to binary. Please try to do that! Your code will be a lot faster! Also, you are putting a regex in while loop's header. Anytime you use regex, it could slow down the code, especially if you put it into a loop that is supposed to go through your code byte-by-byte. Anytime you build a program that runs through some long code byte by byte, then you really have to make it fast. Don't use regex, don't use the "." or ".=" operators. Don't convert back and forth between bases unnecessarily. I see that you are also using the ord() function. That is slower than using vec().

vec() takes three arguments. The first one is the string or character you want to get the byte value from. The second arg is the pointer, and the third arg is usually 8, which tells it to grab an entire byte. Example: $a = vec("Thank you", 0, 8) will grab the letter "T" whose ASCII value is 84, and so $a will contain 84. You can then test if any bits in 84 are on or off using the & operator. if ($a & 1) then you are checking the lowest bit. if ($a & 2), you are checking if the second bit is on. if ($a & 4), you are checking the third bit, and so on... then you can do some arithmetic or bitwise operation on those bits if you want to and plug the new value into a string. I have found that manipulating strings using the vec() function is a lot faster than working with arrays. Creating an array of 10 elements will also take up more memory than creating a string that is 10 bytes long. You can address any byte or word in a string as if it were an array using the vec() function. vec("ABCDEF", 0, 16) will pretend that this string is an array of words, and it will grab the first word. (A word is a 16-bit integer.) So, it will grab "AB" and the value of AB is 0x4142, which is 16706.

vec() can also be used to overwrite a string without modifying its length. Every time you use the "." operator to merge strings, it's going to copy strings in the memory and possibly reserve more memory and then free up some memory. When you use vec() to write to a string, it is very fast! Here is an example : $a = 'hello!'; vec($a, 0, 8) = 72; This code simply overwrites the first byte of string $a. It replaces the first byte with a capital "H." It's kind of like using pointers in C language. This is a very cool feature in Perl. So, whenever you want to take a value from a string and modify it and write it back, use vec(). Use vec() whenever possible! It makes your code faster!


In reply to Re: How to find out, why my perl code is slow. by harangzsolt33
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.