<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 On a hunch, I made two separate cases in this implementation of mean. I wanted to support any iterable, which the second case does. But, if we have a len method, we can use the built-in sum function.
Step1:  1st case
Step2:  2nd case
Step3:  But, is it really worth it to have a separate implementation of the mean function for Sequences? Let's try it and see.
Step4:  Now, time a bunch of runs of the mean function on sequences of different sizes. This takes 30 seconds or so.
<ASSISTANT_TASK:>
Python Code:
def mean(a):
    if isinstance(a, Sequence):
        return sum(a)/float(len(a))
    else:
        s = n = 0
        for x in a:
            s += x
            n += 1
        return s/float(n)
Explanation: On a hunch, I made two separate cases in this implementation of mean. I wanted to support any iterable, which the second case does. But, if we have a len method, we can use the built-in sum function.
End of explanation
mean([random.random() for i in range(100000)])
Explanation: 1st case
End of explanation
mean(random.random() for i in range(100000))
Explanation: 2nd case
End of explanation
def mean_loop(a):
    s = n = 0
    for x in a:
        s += x
        n += 1
    return s/float(n)
def mean_seq(a):
    return sum(a)/float(len(a))
n=1000
sizes = [1000,2000,3000,5000,10000,20000,50000,70000,100000]
cases = [('loop',  'mean_loop(a)'),
         ('sum',   'mean_seq(a)'),
         ('if',    'mean(a)')]
df = pd.DataFrame(index=sizes, columns=('sizes',)+tuple(key for key,cmd in cases))
df.sizes = sizes
Explanation: But, is it really worth it to have a separate implementation of the mean function for Sequences? Let's try it and see.
End of explanation
for size in sizes:
    a = tuple(random.random() for i in range(size))
    for key, cmd in cases:
        t = timeit.timeit(cmd, number=n, globals=globals())
        df.set_value(size, key, t)
df
import seaborn as sns
import matplotlib.pyplot as plt
ax = sns.regplot(x='sizes', y='loop', data=df, label='loop')
ax = sns.regplot(x='sizes', y='sum', data=df, label='sum')
ax = sns.regplot(x='sizes', y='if', data=df, label='if')
plt.ylabel('seconds')
plt.xlabel('sequence length')
plt.title('Running time to find mean of a sequence {} times'.format(n))
plt.legend(loc='upper left')
Explanation: Now, time a bunch of runs of the mean function on sequences of different sizes. This takes 30 seconds or so.
End of explanation
<END_TASK>