in reply to "my" cost

Creating a variable and assigning a value takes more time than assigning a value to an existing variable. That seems very reasonable. Is 0.00722599029541 seconds really a long time?

Replies are listed 'Best First'.
Re^2: "my" cost
by Bod (Parson) on Aug 18, 2024 at 19:14 UTC
    Is 0.00722599029541 seconds really a long time?

    It is if you need to iterate through the loop enough times!

    However, I don't concern myself with how long things take until they take "too long". At that point, I look to see why they are taking so long and what we can do about it.

      It is if you need to iterate through the loop enough times!

      How do you figure that?

      If you're going to loop a million times, it's still only takes 0.0007 s cumulatively. It will only add up to 7 s if you do a billion passes of the loop. If you're doing something a billion times in Perl, it's going to take an hour, a day, or more. An extra 7 s isn't going to matter. This is the point where you offload the work to C or something, not move a my.

Re^2: "my" cost
by Danny (Chaplain) on Aug 17, 2024 at 01:21 UTC
    Doesn't seem like a long time, but I've always been curious about this. I suppose in some scenarios it could be beneficial to declare the my variable outside the loop.

      I can't imagine any. If you're so desperate that 7 ns per loop pass matters to you, moving a my isn't the solution you need.