Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Is there any alternative way to speed up ?

by jnarayan81 (Sexton)
on Jun 16, 2017 at 14:24 UTC ( [id://1192942]=perlquestion: print w/replies, xml ) Need Help??

jnarayan81 has asked for the wisdom of the Perl Monks concerning the following question:

I am again here for the Perl wisdom. Following script is miserably slow on big data. I am looking for a SMART/aternative way to achieve the same with speed !!

raaaammmmmaaaaaj -> ra3m4a4j and vice versa

use strict; use warnings; my @char; while (<DATA>) { @char = split //; } comp(\@char); #--------------------- my $com= "r0a3m4a4j0"; my @com = split //, $com; dcomp (\@com); #dcomp sub here sub dcomp { my ($com_ref)=@_; my @com=@$com_ref; my $car; for (my $aa=0; $aa<=$#com; $aa++) { if ($com[$aa]!~ /\D/) { print "$car" x ($com[$aa]);} else {print " +$com[$aa]"; $car = "$com[$aa]";} } print "\n"; } #comp sub here sub comp { my ($char_ref)=@_; my @char=@$char_ref; my $cnt=""; for (my $aa=0; $aa<=$#char; $aa++) { if ($char[$aa+1]) { if ($char[$aa] eq $char[$aa+1]) { $cnt++; } elsif ($cnt) { print "$char[$aa]"."$cnt"; undef $cnt;} else {print "$char[$aa]";} } } print "\n"; } __DATA__ raaaammmmmaaaaaj

Replies are listed 'Best First'.
Re: Is there any alternative way to speed up ?
by Eily (Monsignor) on Jun 16, 2017 at 14:39 UTC

    Your code is really hard to read, because it is not well indented (maybe perltidy can help with that), and the variable and function names are far from explicit and kind of all look the same.

    Luckily for you, even though you didn't explain how you go from raaaammmmmaaaaaj to r0a3m4a4j0 and back, the technique is common enough that it is not too hard to spot. This is called run length encoding, there might be a module out there that does that for you but you can also look at the first solution here. In that example the number is put before the character, and there's going to be an offset of 1 with your expected output (raaa will be coded as 1r3a instead of r0a2), but a little tinkering should give you what you want.

Re: Is there any alternative way to speed up ?
by tybalt89 (Monsignor) on Jun 16, 2017 at 15:18 UTC
    #!/usr/bin/perl # http://perlmonks.org/?node_id=1192942 use strict; use warnings; while(<DATA>) { print /(.*)/, " -> ", /\d/ ? s/(\D)(\d+)/$1 x ($2 + 1)/ger : s/(\D)\K(\1+)/ length $2 /ger; } __DATA__ raaaammmmmaaaaaj ra3m4a4j foobar really?
Re: Is there any alternative way to speed up ?
by karlgoethebier (Abbot) on Jun 16, 2017 at 16:20 UTC

    Good formatting is half the job:

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    Furthermore I consider that Donald Trump must be impeached as soon as possible

      An unambiguous specification helps a bit, too: "raaaammmmmaaaaaj -> ra3m4a4j and vice versa" explicit in the OP text,  my $com= "r0a3m4a4j0"; implicit in the code. :)


      Give a man a fish:  <%-{-{-{-<

        "An unambiguous specification helps a bit, too..."

        Definitely. But I had another basic theme. If you know what a basic theme is ;-)

        Reading well formatted code is a bit similar to reading a well typesetted book or score: It's a pleasure.

        Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        Furthermore I consider that Donald Trump must be impeached as soon as possible

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1192942]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-03-29 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found