On 7/19/07, <b class="gmail_sendername">Eric Brunson</b> <<a href="mailto:brunson@brunson.com">brunson@brunson.com</a>> wrote:<div><span class="gmail_quote"></span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Tino Dai wrote:<br>> On 7/19/07, *Kent Johnson* <<a href="mailto:kent37@tds.net">kent37@tds.net</a> <mailto:<a href="mailto:kent37@tds.net">kent37@tds.net</a>>><br>> wrote:<br>><br>> > The two advantages that I can see are, I don't need to type as
<br>> much, and<br>> > there would be a speed up in the execution of code.<br>><br>> Why do you expect a speedup?<br>><br>><br>> In the Python Reference by David Beazley on p. 40, he substituted
<br>> import math<br>> with from math import sqrt and he switched out d = d + math.sqrt(i) with<br>> sqrt(i). He said that that change made the program run twice as fast.<br>> So therefore<br>> I was under the impression that "from somemodule import someclass"
<br>> would always be<br>> faster than import somemodule.<br><br>The reason it's faster is because to get the actual function for<br>math.sqrt() after importing math, you have to do a dictionary lookup in<br>the "math" namespace. I don't think it should run twice as fast. I
<br>would only worry about it if you had some situation where you were using<br>the same name over and over:<br><br>import math<br>for i in range( 0, 1000000000 ):<br> x = math.sqrt(i)<br><br>That's 1000000000 dictionary lookups. Importing just the sqrt name
<br>would avoid those lookups, so would:<br><br>import math<br>f = math.sqrt<br>for i in range( 0, 1000000000 ):<br> x = f(i)<br><br>Does that make sense?</blockquote><div><br>I guess there is no silver bullet. Thanks for that! It makes total sense
<br>now that you mention it.<br><br>-Tino<br> <br></div><br></div><br>