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

Re^5: I have a list expanding when I don't want it to

by GrandFather (Sage)
on Nov 20, 2014 at 00:20 UTC ( #1107844=note: print w/replies, xml ) Need Help??


in reply to Re^4: I have a list expanding when I don't want it to
in thread I have a list expanding when I don't want it to

First make it work (using the easiest to maintain code you can) then, only if you need to, make it faster.

"Need to" requires that you have performed some sort of profiling so you know where things are slow. And before you "speed it up" write unit tests to make sure your speed up isn't at the expense of breaking the code (doing nothing can often be quite fast).

Perl is the programming world's equivalent of English
  • Comment on Re^5: I have a list expanding when I don't want it to

Replies are listed 'Best First'.
Re^6: I have a list expanding when I don't want it to
by Lady_Aleena (Curate) on Nov 20, 2014 at 04:55 UTC

    GrandFather, I added the first sentence to the list of Words of Wisdom (inelegantly called Notes from others) on my scratchpad so I will see them every time I look at it. Thanks for the nudge.

    No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
    Lady Aleena
Re^6: I have a list expanding when I don't want it to
by BrowserUk (Pope) on Nov 20, 2014 at 00:51 UTC

    I down-voted you.

    Why?

    Because you are telling her, how she should program her code.

    And, (BTW) assuming (arrogantly) that she hasn't/didn't already profile both possibilities and choose appropriately.

    No attempt to help her. Just see the word (fast/er/est) and knee-jerk into patronizing dick mode.

    Subtext

    Assigning () to an array, causes the array to be emptied; but the AV* is retained; so is the hash key holding the reference to the AV*. Thus the HV* is unmodfied.

    Deleting the key(s) from the hash, not only empties (GCs) the elements of the array and modifies the size of the AV*; it also modifies the hash by removing a key value pair, only to recreate it immediately.

    She made the right choice. And I'm betting it wasn't by either guesswork or knee-jerk.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      BrowserUk sweetie, just so you know, GrandFather has helped me a lot over the years, so he kind of knows my quirks. What you saw as a knee-jerk reaction, was him seeing me saying something (in this case "faster") which is more than likely a alarm bell in his head actually. I am known to try to prematurely optimize my code. There are a few people on here who have told me not to do it, but I still do anyway. He chides me, I think, because he wants to remind me of one of my bad habits. Now you know about it, I am sure you will more than likely gently nudge me now and again if you see me doing it in the future.

      As to my motivation to empty over delete, it is quite simple. I read in delete's doc "But both of these are slower than just assigning the empty list or undefining %HASH or @ARRAY:", so I looked at the suggestion below it, and used it instead. I didn't do any fancy testing (since I don't get testing as you know it) or whatever profiling is. (I think GrandFather was also trying, again, to push me into teaching myself testing which I tend to avoid because of the complexity of it. That is another discussion for another time.)

      Now, would you please do me a teeny tiny favor and expand your abbreviations (AV*, HV*, and GCs)? I don't want to get it wrong in my head.

      You can relax. GrandFather and others may look like they are coming on strong with their opinions to me, and most likely, it is to push me into the right direction when I appear bound and determined to go wrong. (In some rare cases their pushing worked actually.) So sit back and have a cookie. :)

      No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
      Lady Aleena

      I really don't want to get into a long debate about this but:

      Because you are telling her, how she should program her code.

      Isn't that in large part what Perlmonks is about? Or at least (because there is no code involved in this) offering well meant guidance.

      And, (BTW) assuming (arrogantly) that she hasn't/didn't already profile both possibilities and choose appropriately.

      Well, no actually. LA said "I thought about using delete, however the doc suggested emptying the lists would be faster.". No code involved. No test to see if there was a real issue. No consideration of which implementation might be easier to understand and maintain.

      No attempt to help her. Just see the word (fast/er/est) and knee-jerk into patronizing dick mode.

      Well, no actually. The advice offered was targeting the initial focus on speed instead of code clarity. Shaving a few micro seconds off something that only happens a few hundred times per execution of the script is almost certainly irrelevant.

      In this particular case the code difference is likely to be small, but as a general thing concern over "the best way" can either make it almost impossible to make progress, or lead to very hard to maintain code that causes grief for ever. At $work an early coding decision for a C++ string object stores the actual string in a fashion that the debugger can't easily get at. At the time that probably made some string manipulations very slightly faster. Now compiler improvements mean that there is likely no performance advantage, but we get a major debugging disadvantage every time we want to see the contents of a string variable. The application using the string class does very little string manipulation so there is no performance improvement seen by users. The cost in developer time of that decision probably amounts to thousands of dollars.

      So, I stand by my advice to LA and anyone else who bothers to read this far: Make it work first, then make it faster if you need to.

      Perl is the programming world's equivalent of English

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1107844]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2019-02-17 00:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I use postfix dereferencing ...









    Results (95 votes). Check out past polls.

    Notices?