speed questions - Grasshopper2024-03-29T00:21:56Zhttps://www.grasshopper3d.com/forum/topics/speed-question?commentId=2985220%3AComment%3A805112&feed=yes&xn_auth=noThere's also the general Pyth…tag:www.grasshopper3d.com,2013-03-11:2985220:Comment:8059842013-03-11T09:50:46.727ZAnders Holden Deleuranhttps://www.grasshopper3d.com/profile/AndersHoldenDeleuran
<p>There's also the general <a href="http://docs.python.org/2/library/copy.html" target="_blank">Python copy functions</a>. However I believe that those methods (particularly deepcopy) may be slower than what we've already covered, at least for lists.<br/><br/>:D</p>
<p>There's also the general <a href="http://docs.python.org/2/library/copy.html" target="_blank">Python copy functions</a>. However I believe that those methods (particularly deepcopy) may be slower than what we've already covered, at least for lists.<br/><br/>:D</p> Interesting! I didn't know th…tag:www.grasshopper3d.com,2013-03-10:2985220:Comment:8057062013-03-10T22:40:41.703ZMostapha Sadeghipour Roudsarihttps://www.grasshopper3d.com/profile/MostaphaSadeghipour
<p>Interesting! I didn't know that list() makes a copy of the list. I was only using it to convert tuple to list! Thanks Anders for posting.</p>
<p>PS: Do you have a Python cheat sheet somewhere on your blog? You should post one if you haven't done it already! ;)</p>
<p>Interesting! I didn't know that list() makes a copy of the list. I was only using it to convert tuple to list! Thanks Anders for posting.</p>
<p>PS: Do you have a Python cheat sheet somewhere on your blog? You should post one if you haven't done it already! ;)</p> In continuation of that, I'm…tag:www.grasshopper3d.com,2013-03-09:2985220:Comment:8054832013-03-09T22:45:44.699ZAnders Holden Deleuranhttps://www.grasshopper3d.com/profile/AndersHoldenDeleuran
<p>In continuation of that, I'm not really sure if any memory benefits of modifying lists in place outweighs the potential problems this may cause (i.e. losing track of what's what ID-wise). It would be interesting to examine this more in-depth actually. Either way, a few more Pythonic nuggets related to your examples above. Apologies if these are obvious!<br></br><br></br>The first case (adding to a list) can be written like so:<br></br><br></br><em>numberList1 = [1,2,3,4,5,6,6,7,8]<br></br></em><em>numberList2 =…</em></p>
<p>In continuation of that, I'm not really sure if any memory benefits of modifying lists in place outweighs the potential problems this may cause (i.e. losing track of what's what ID-wise). It would be interesting to examine this more in-depth actually. Either way, a few more Pythonic nuggets related to your examples above. Apologies if these are obvious!<br/><br/>The first case (adding to a list) can be written like so:<br/><br/><em>numberList1 = [1,2,3,4,5,6,6,7,8]<br/></em><em>numberList2 = [i+2 for i in numberList1]<br/></em><em>print numberList2<br/></em><br/>The second case (copying a list) can be performed using the list() function:<br/><br/><em>list1 = [1,2,3,4]</em><br/><em>list2 = list(list1)</em><br/><em>list2[0] = 0</em><br/><em>print list1<br/><br/></em>Or by using the slice notation:<br/><br/><em>list1 = [1,2,3,4]</em><br/><em>list2 = list1[:]</em><br/><em>list2[0] = 0</em><br/><em>print list1</em></p>
<p></p> Got it. I should have mention…tag:www.grasshopper3d.com,2013-03-09:2985220:Comment:8051122013-03-09T00:24:22.476Zpersonhttps://www.grasshopper3d.com/profile/person
<p>Got it. I should have mentioned that my most recent post was a more general observation of list behavior in python.</p>
<p>Got it. I should have mentioned that my most recent post was a more general observation of list behavior in python.</p> Kendra,
What I was trying to…tag:www.grasshopper3d.com,2013-03-09:2985220:Comment:8051102013-03-09T00:20:46.088ZMostapha Sadeghipour Roudsarihttps://www.grasshopper3d.com/profile/MostaphaSadeghipour
<p>Kendra,</p>
<p>What I was trying to say is how lists work in Python. My understanding from your description was that you see the changes in the original list that you don't want to happen. Is that right? <span style="font-size: 13px;">What I mentioned above is why this is happening and how you can solve it. </span><span style="font-size: 13px;">Here is another example:</span></p>
<p>list1 = [1,2,3,4] <br></br>list2 = list1</p>
<p>list2[0] = 0</p>
<p>However I changed the first item in list2 if…</p>
<p>Kendra,</p>
<p>What I was trying to say is how lists work in Python. My understanding from your description was that you see the changes in the original list that you don't want to happen. Is that right? <span style="font-size: 13px;">What I mentioned above is why this is happening and how you can solve it. </span><span style="font-size: 13px;">Here is another example:</span></p>
<p>list1 = [1,2,3,4] <br/>list2 = list1</p>
<p>list2[0] = 0</p>
<p>However I changed the first item in list2 if you print list1 it will return [0,2,3,4]! as I said they are the same list. If you want the changes in list2 doesn't effect list1 you should do something like this:</p>
<p>list1 = [1,2,3,4]<br/>list2 = []<br/>[list2.append(i) for i in list1]<br/>list2[0] = 0</p>
<p>print list1 will return [1,2,3,4]</p>
<p></p>
<p><span style="font-size: 13px;">I think it will be helpful if you can post your script. Hope it helps.</span></p>
<p><span style="font-size: 13px;">-Mostapha</span></p>
<p></p> Thanks Mostapha, part of a ge…tag:www.grasshopper3d.com,2013-03-09:2985220:Comment:8049072013-03-09T00:07:10.827Zpersonhttps://www.grasshopper3d.com/profile/person
<p>Thanks Mostapha, part of a generation of home schooled coders here.</p>
<p></p>
<p>So, let's say I have a list of numbers called <em>numberList</em>, and I then pipe <em>numberList</em> through a definition that adds the value 2 to each number in <em>numberList.</em> From what you are saying, it would be more memory efficient to code: <em>numberList = addTwo(numberList) </em> rather then <em> </em>numberListPlus2 = addTwo(numberList) because you would not be creating an additional list.…</p>
<p>Thanks Mostapha, part of a generation of home schooled coders here.</p>
<p></p>
<p>So, let's say I have a list of numbers called <em>numberList</em>, and I then pipe <em>numberList</em> through a definition that adds the value 2 to each number in <em>numberList.</em> From what you are saying, it would be more memory efficient to code: <em>numberList = addTwo(numberList) </em> rather then <em> </em>numberListPlus2 = addTwo(numberList) because you would not be creating an additional list. Is this accurate?<em><br/></em></p>
<p></p>
<p><em>def addTwo (list) :</em></p>
<p><em> temp = [] </em></p>
<p><em> for i in range(len(list)) :</em></p>
<p><em> a = list[i] + 2</em></p>
<p><em> temp.append(a)</em></p>
<p><em> return temp</em></p>
<p></p>
<p><em>numberList = [1,2,3,4,5,6,6,7,8 etc. ]</em></p>
<p><em>#numberListPlus2 = addTwo(numberList) ## This makes an additional list</em></p>
<p><em><em>numberList = addTwo(numberList) ## This writes over the existing numberList</em></em></p> Kendra,
What you get is norma…tag:www.grasshopper3d.com,2013-03-08:2985220:Comment:8049032013-03-08T23:42:56.572ZMostapha Sadeghipour Roudsarihttps://www.grasshopper3d.com/profile/MostaphaSadeghipour
<p>Kendra,</p>
<p>What you get is normal since in python both lists are referring to the same place somewhere in the memory (they are the same list!). You should make a new copy of the list if you want the changes not to effect the first list. Something like:</p>
<p><span style="font-size: 13px;">list2 = []; </span><span style="font-size: 13px;">[list2.append(item) </span><span style="font-size: 13px;">for item in list1]</span></p>
<p><span style="font-size: 13px;">and then change the items in…</span></p>
<p>Kendra,</p>
<p>What you get is normal since in python both lists are referring to the same place somewhere in the memory (they are the same list!). You should make a new copy of the list if you want the changes not to effect the first list. Something like:</p>
<p><span style="font-size: 13px;">list2 = []; </span><span style="font-size: 13px;">[list2.append(item) </span><span style="font-size: 13px;">for item in list1]</span></p>
<p><span style="font-size: 13px;">and then change the items in list2.</span></p>
<p><span style="font-size: 13px;">As a side note print is so expensive</span> <span style="font-size: 13px;">computationally </span><span style="font-size: 13px;">and can slow down the process. Use it carefully or it can slow down the process itself.</span></p>
<p>-Mostapha</p> That's all great to know, Dav…tag:www.grasshopper3d.com,2013-03-08:2985220:Comment:8047822013-03-08T23:33:36.741Zpersonhttps://www.grasshopper3d.com/profile/person
<p>That's all great to know, David, thank you for the post. I will look into the stopwatch class and see what I can learn. Is there also a way to keep track how much memory your using per definition or per array?</p>
<p></p>
<p>I did run into an unusual instance recently when I sent data in the form of a list from one python component to another, and then used <em>list.del</em> and <em>list.pop</em> to delete items from that list. The data in the previous node was changed. I ran a <em>print…</em></p>
<p>That's all great to know, David, thank you for the post. I will look into the stopwatch class and see what I can learn. Is there also a way to keep track how much memory your using per definition or per array?</p>
<p></p>
<p>I did run into an unusual instance recently when I sent data in the form of a list from one python component to another, and then used <em>list.del</em> and <em>list.pop</em> to delete items from that list. The data in the previous node was changed. I ran a <em>print len(list) </em>in the python node earlier in the chain before and after using <em>list.del</em> and <em>list.pop</em> in the next python node in the chain. I noticed that the length of the list in the python node earlier in the chain shrank to match the effect of the <em>list.del</em> / <em>list.pop </em>that was run in the next python node. I will try to replicate and post code.</p> Thanks Anders. I've been wond…tag:www.grasshopper3d.com,2013-03-08:2985220:Comment:8047762013-03-08T23:24:08.477Zpersonhttps://www.grasshopper3d.com/profile/person
<p>Thanks Anders. I've been wondering how RhinoCommon compared to rhinoscriptsyntax. I typically use a mix of both but it sounds like RhinoCommon might be the way to go.</p>
<p>Thanks Anders. I've been wondering how RhinoCommon compared to rhinoscriptsyntax. I typically use a mix of both but it sounds like RhinoCommon might be the way to go.</p> The following does not direct…tag:www.grasshopper3d.com,2013-03-08:2985220:Comment:8049422013-03-08T19:48:11.817ZAnders Holden Deleuranhttps://www.grasshopper3d.com/profile/AndersHoldenDeleuran
<p>The following does not directly answer your questions, but maybe be helpful if you're experiencing slow execution speeds in a Python component in general:<br></br><br></br>If you are using the Rhinoscriptsyntax library this may substantially slow things down as compared to straight up RhinoCommon (<a href="http://www.grasshopper3d.com/forum/topics/points-along-a-curve-python?xg_source=activity" rel="nofollow" target="_blank">see this thread</a>).<br></br><br></br>Beyond that it sounds like you might have…</p>
<p>The following does not directly answer your questions, but maybe be helpful if you're experiencing slow execution speeds in a Python component in general:<br/><br/>If you are using the Rhinoscriptsyntax library this may substantially slow things down as compared to straight up RhinoCommon (<a rel="nofollow" href="http://www.grasshopper3d.com/forum/topics/points-along-a-curve-python?xg_source=activity" target="_blank">see this thread</a>).<br/><br/>Beyond that it sounds like you might have some bottlenecks and/or "logical errors" in your scripts which are hard to guess without any code. As David suggests you probably want to profile the code to find these (I put up an example file on how to do this in the other thread).<br/><br/>Hope that helps..</p>