python speed composed functions
<p>I'd like to understand why I get the speed differences that I see when I compose functions in different ways.
I'm mostly interested in understanding why case (1) is faster than case (2).</p>

<p>Thanks!</p>

<pre><code>import math
from time import time

def f(x):
    return 2*x
def g(x):
    return x*x
def h(x):
    return math.sqrt(x)

time0 = time()
for x in range(1,10**7):
    x_ = h(g(f(x)))
print 'h(g(f(x))): {}'.format( time() - time0)

def fgh(x):
    return h(g(f(x)))
time0 = time()
for x in range(1,10**7):
    x_ = fgh(x)
print 'composed: {}'.format( time() - time0)

time0 = time()
for x in range(1,10**7):
    x_ = f(x)
    x__ = g(x_)
    x___ = h(x__)
print 'subsequent: {}'.format( time() - time0)
</code></pre>

<p>As runtimes, I get:</p>

<pre><code>h(g(f(x))): 2.83475399017
composed: 3.29999113083
subsequent: 3.4387819767
</code></pre>