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

PERL is really good for writing the kind of string/file parsing programs that I usually need to do.

What I really love is the insignificant amount of time it takes me to write quick scripts and throwaway code, compared to C/C++/JAVA.

However, I want to learn how to speed things up.

For example, I would want to learn how to give hints to PERL so that it can make some decisions better - specially things related to strings.

It seems to me that perl copies a string whenever you do anything, regardless of whether you really modify the copy later or not?

Is this by design (and can I turn that away using some magic?) or am I ranting?

I really want to treat some strings as (const char *) - I am sure we always do not need everything to be a std::string with all its baggage involved (let's assume std::string to be analogous to PERL string)

Can I hint PERL to do that on some strings?

I remember reading in some article (please comment if you can place it) that you can hint PERL that you will not modify some variable and thus it removes the extra baggage that is otherwise required if you were to modify it etc?

I believe PERL variables have two internal pointers to a same PERL variable - one can store a number and another a string (array of characters) - could we always ask PERL to choose one throughout?

Could we make PERL treat some strings as (const char *) so that they do not tag around functionality required to modify them?

For example, I read somewhere (maybe the same article?) that unpack() is faster than substr() because substr() returns a lvalue, so that you can operate on it as well.

For example, if you wanted to replace the first two characters of a string with 'ef', you could write:

substr(string, 0, 2) = 'ef'; # string now begins with 'ef'

Hence, unless you are using this special feature of substr(), you are better off using substr?

Did I just rant all the way through?

Replies are listed 'Best First'.
Re: Hints to PERL
by roboticus (Chancellor) on May 18, 2009 at 23:18 UTC
    PoorLuzer:

    Are you sure it's too slow? Don't try to make something faster until you know:

    1. that it's not fast enough, and
    2. which parts of your code are too slow.

    Or in other words, "you can't improve it if you don't measure it". (Sure, it's an oversimplification...)

    ...roboticus

Re: Hints to Perl
by jettero (Monsignor) on May 18, 2009 at 22:14 UTC
    If you want to do that kind of optimization for some things that really really need the speed boost, look at perlxs, perlguts, and hack away. Another choice is Inline::C.

    But almost all the time it isn't worth it and Perl (not PERL) doesn't support this kind of stuff in any meaningful way. It's a high level language precisely so you don't have to bother with that kind of micro-management.

    -Paul

Re: Hints to PERL
by Anonymous Monk on May 19, 2009 at 08:14 UTC
    It seems to me that perl copies a string whenever you do anything, regardless of whether you really modify the copy later or not?
    No, perl copies strings on assignment ($foo = $bar). Example:
    #!/usr/bin/perl -- use strict; use warnings; my $str = "foo"; my $copy = $str; print "$copy $str\n"; nocopy($str); yescopy($str); print "$copy $str\n"; # now $str is foobar sub nocopy { $_[0] .= 'bar'; '...' } sub yescopy { my ($copy) = $_[0]; $copy = shift; $copy .= 'bar' } __END__ foo foo foo foobar

    that you can hint PERL that you will not modify some variable and thus it removes the extra baggage that is otherwise required if you were to modify it etc?

    You are probably thinking of constant and/or making variables readonly, which forces you not to write code that try to modify those variables, as a result there is no unneeded copying

    ... PERL ...

    The Perl community likes to call it Perl, or perl, but not so much PERL, even if its a fun bacronym generator.

      ... PERL ...
      The Perl community likes to call it Perl, or perl, but not so much PERL
      I realise this is off the main topic, but I have a couple of issues of (sic) THE PERL REVIEW (small caps, large caps, small caps) on my desk and it always jars me when I see that. Particularly because that may be something that a SoPW might come across while Seeking, and therefore it might seem to give validity to that format.

      (In an attempt to be fair to brian_d_foy, it is only thus on the cover. Inside, all references to the language look Perlish.)


      Because of the economic downturn, there will be no signatures this Christmas
Re: Hints to PERL
by nikosv (Deacon) on May 19, 2009 at 09:28 UTC
    Check Why Not Translate Perl to C? which shows that the difference in speed is neglible;or you must resort to trickery.At the bottom line you should ask : is it worth the trouble?