in reply to Re^2: What to test in a new module
in thread What to test in a new module

You can... but concatenating is faster

I hadn't thought of the speed implications. But I guess concatenation is always going to be faster than interpolation.

Replies are listed 'Best First'.
Re^4: What to test in a new module
by Arunbear (Prior) on Jul 09, 2023 at 11:16 UTC
    I usually prefer interpolation because I can see at a glance what the result will look like, so there is less mental work to do when reading it.

    sprintf is a good middle ground between interpolation and concatenation. I sometimes use that when the variable names are long enough to interfere with the readability of interpolation.

    my $signed_payload = $sig_head{'t'} . '.' . $self->{'payload'}; my $signed_payload = sprintf '%s.%s', $sig_head{'t'}, $self->{'payload +'};

      Also:

      my $signed_payload = join '.', $sig_head{'t'}, $self->{'payload'};
Re^4: What to test in a new module
by kcott (Archbishop) on Jul 09, 2023 at 10:51 UTC

    ++ My response may come across as negative. That's not my intention so I'm just letting you know that I upvoted your post before replying. :-)

    "I hadn't thought of the speed implications."

    Unless you're experiencing noticeable speed issues, continue to not think about it. This sort of micro-optimisation is rarely useful: you'll spend inordinate amounts of time possibly saving just some nanoseconds when it would be more productive to focus on the code in general.

    "But I guess concatenation is always going to be faster than interpolation."

    Don't guess; instead, Benchmark.

    I seem to recall 20+ years ago, when coding to v5.6, various forms of concatenation (e.g. join '', $x, $y or $x . $y or "$x$y" or others) provided certain performance benefits. Reading the optimisation sections of release notes over the years has shown that these benefits have largely dissipated.

    In the main, I would focus on readability. For instance, many authors eschew whitespace, resulting in code like $x.'.'.$y which I find difficult to read at first glance, and often require me to do a doubletake to determine what each dot is doing.

    This advice pertains to many aspects of Perl syntax, not just concatenation, which perhaps had some benefits in the past but have been optimised away as the language has matured.

    — Ken

      Unless you're experiencing noticeable speed issues, continue to not think about it (speed)

      Don't guess; instead, Benchmark

      I was guessing because I wasn't dwelling on speed :)

      Unless I have to process lots of something with a big loop, I don't tend to think about speed unless it becomes an issue - my guess was about academic interest rather than practical concern.

      Thanks for the benchmarking link - I have intended to try benchmarking something just so I know how to do it.