[Tutor] Re: What is "in-line code"?

Kent Johnson kent_johnson at skillsoft.com
Sat Aug 14 14:54:45 CEST 2004


Obviously Lee's answer is different than mine. I think Lee is right =:-)
Lee's answer is definitely the conventional meaning of in-line. I thought 
from the context that Guido was talking about Python vs C but on second 
reading I see Lee's interpretation also works.

That said, it is still true that for optimization, you should prefer 
functions written in C over functions written in Python. Calling a Python 
function in a loop is slower than in-line code in a loop is slower than 
moving the whole loop to C.

Kent

At 05:13 PM 8/14/2004 +0430, Lee Harr wrote:
"In-line" just means that the code is written out right there
>in the body of the loop, instead of being in a separate
>function which the loop calls.  For example ...
>
># Not In-line
>>>>def add2(a, b):
>...     return a + b
>...
>>>>pairs = [(1, 2), (3, 4), (5, 6)]
>>>>for pair in pairs:
>...     print add2(pair[0], pair[1])
>...
>3
>7
>11
>
>
># Same result with In-line code
>>>>for pair in pairs:
>...     print pair[0] + pair[1]
>...
>3
>7
>11
>
>
>So, if what you are doing is very simple, you might choose not to
>separate out add2() and just write the code in-line.
>
>In python, functions calls are relatively "heavy" and if the pairs
>list has many many elements you could get a noticeable speedup.
>
>C compilers (and probably other languages too...) allow you to mark
>some functions or methods as "in-line", which is nice since it allows
>you to show the code as a separate entity (which helps maintenance)
>but get the speed-up of actually copying the code in to the body
>of the loop.
>
>A different (but related?) optimization would be "unrolling" where the
>code of the for loop is actually copied over and over instead of
>wasting the time to jump back to the beginning of the code for the
>next iteration.
>
>_________________________________________________________________
>Protect your PC - get McAfee.com VirusScan Online 
>http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
>
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list