Hi David

It would be worth nailing down which bit is slow. I'm guessing it's the
for-loop rather than the unpack but I'd make sure of it using something
like this

https://metacpan.org/pod/Time::HiRes

If it is indeed the for-loop, I'd experiment with using regex substitution
instead of the for-loop as regex code is compiled once at runtime.

> $content.chr

Hmm. That looks like a bug to me. To make sure you're not shooting yourself
in the foot,

use strict;
use warnings;

until you're sure the code is correct and you can take them out for
performance testing and see if it makes a difference. I doubt it.

Finally, you might want to play with MCE

http://perltricks.com/article/61/2014/1/21/Make-your-code-run-faster-with-Perl-s-secret-turbo-module

Andrew


On Thu, Jul 30, 2015 at 9:14 PM, 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);
> 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) {
>     my $char= $YENC_CHAR_MAP[$hexChar];
>
>         #null || LF || CR || =
>     if ($char == 0 || $char == 10 || $char == 13 || $char == 61 ||
>
>         # TAB || SPC
>         (($char == 9 || $char == 32) && ($column == $YENC_NNTP_LINESIZE
>         || $column==0)) ||
>
>         ($char==46 && $column==0) # .
>         ) {
>
>       $content =$content. '=';
>       $column+=1;
>
>       $char=($char + 64);#%256;
>     }
>
>     $content = $content.chr $char;
>
>     $column+=1;
>
>     if ($column>= $YENC_NNTP_LINESIZE ) {
>       $column=0;
>       $content = $content."\r\n";
>     }
>
>   }
>
>   return $content;
> }
>
> ####
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v2
>
> iQEcBAEBCAAGBQJVuoW/AAoJEJ/OLjwuDYzKvMcH/2Q2B1p3L+Q/iPvjfeTNoQfX
> V+MsLkSelzjl/rKKRnYgYerHacaTpWxy6sKKnSlTQy2c2XXIXOLLxKZxHjw869bA
> hwnlKrl2UnABekJF270J7wIk0K6+zw1BTjHjcPlibfBPTCX6lOoaO0PHy5cHycXC
> XQ3+Pjo3e7Ux7dx16vFJq/XJl70LmV5CFShvQoLRtSV3fxvOEE25uGzRm6zCEVSd
> cEISGgLXBHwzvpU5+ma4SIuXDcYWDpfNOUukPF7zLHtn+WEjr/CImcM75MvnjUtg
> CZn9SIwgOeeNZ22T5SbOitX5uhqyFGOln8Y8DOcHjTCKTD1uem//IclcqZUrXBU=
> =r+/T
> -----END PGP SIGNATURE-----
>



-- 
Andrew Solomon

Mentor@Geekuni http://geekuni.com/
http://www.linkedin.com/in/asolomon

Reply via email to