Hi David,

see below for my comments on your code.

On Thu, 30 Jul 2015 22:14:55 +0200
David Emanuel da Costa Santiago <deman...@gmail.com> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> 
> Hello,
> 
> I'm developing a perl yenc encoder but unfortunatelly it's not having
> the performance i was expecting to have (and it's CPU heavy).
> 
> Can i get some help improving it's performance?
> 
> This is the code i have (41 lines).
> 
> Initially i though about memoize it, but unfortunately i don't think
> it's possible.
> 
> Replacing the for loop with a map, would be a good option? Does anybody
> has any idea to improve it's performance?
> 
> 
>  
> Regards,
> David Santiago
> 
> 
> CODE: The following function receives a binary string.
> 
> ####
> 
> my @YENC_CHAR_MAP = map{($_+42)%256;} (0..0xffff);

$X % 256 can also be written as ($X & 0xFF) (and should be a bit faster). Also
note that you can easily use (@LIST) x $N to repeat it here.

> my $YENC_NNTP_LINESIZE=128;
> 
> sub _yenc_encode{
>   my ($string) = @_;
>   my $column = 0;
>   my $content = '';
> 
>   my @hexString = unpack('W*',$string); #Converts binary string to hex
>  
>   for my $hexChar (@hexString) {

Since @hexString is used only once, you can put it inside the for :

        for my $hexChar (unpack('W*',$string)) {

>     my $char= $YENC_CHAR_MAP[$hexChar];
> 
>       #null || LF || CR || =
>     if ($char == 0 || $char == 10 || $char == 13 || $char == 61 ||
> 

Since you have many $char == $X || $char == $Y , you can convert it to a hash
or in your case using perldoc -f vec may do the trick:

http://perldoc.perl.org/functions/vec.html

>       # TAB || SPC
>       (($char == 9 || $char == 32) && ($column == $YENC_NNTP_LINESIZE
>       || $column==0)) || 
> 
>       ($char==46 && $column==0) # . 
>       ) {
>       
>       $content =$content. '=';

This can be written as:

        $content .= '=';

>       $column+=1;

        $column++;

>       
>       $char=($char + 64);#%256;

        $char += 64;

>     }
> 
>     $content = $content.chr $char;

        $content .= chr($char);

>     
>     $column+=1;

        $column++;

>     
>     if ($column>= $YENC_NNTP_LINESIZE ) {
>       $column=0;
>       $content = $content."\r\n";

        $content .= "\r\n";

And note that \r\n is not very portable:

>     }
> 
>   }
> 
>   return $content;
> }
> 

Finally, note that you may opt to write such speed-critical code in C or C++ and
bind it to Perl using XS or Inline::C :

* https://metacpan.org/pod/distribution/Inline-C/lib/Inline/C.pod

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang

Chuck Norris can only convince you that you're deceased. Summer Glau can
also convince you that you're alive, which is much harder.
    — http://www.shlomifish.org/humour/bits/facts/Summer-Glau/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to