有没有比这更好的方法来进行运行时分析了?

时间:2019-04-04 17:55:40

标签: python python-3.x performance time data-analysis

我目前有2个不同的函数,可以对它们进行矢量化处理:acc_rej_sine(max_iter, algorithm=None)analytical_sine(max_iter, algorithm=None),我正尝试将其运行时间与计算的迭代次数进行比较。即比较所有4种方法; 2个循环,2个向量化。基本上我的代码是这样的:

def analytical_sine(max_iter, algorithm=None):
    if algorithm is None:
        count = 0
        analytical_hist = []
        for i in range(max_iter):
            count += 1
            progress = round((count/max_iter)*100)
            sys.stdout.write('\r' + str(progress) + '%')
            uni_dist = np.random.uniform(0, 1)
            arccos = np.arccos(1 - 2*uni_dist)
            analytical_hist.append(arccos)
    elif algorithm is "vectorise":
        analytical_hist = np.arccos(1 - 2*np.random.uniform(0, 1, max_iter))

    return analytical_hist

def acc_rej_sine(max_iter, algorithm=None):
    x = np.random.uniform(0, np.pi, max_iter)
    y = np.random.rand(max_iter)
    if algorithm is None:
        accepted_x = []
        j = count = 0
        for i in range(max_iter):
            count += 1
            progress = round((count/max_iter)*100)
            sys.stdout.write('\r' + str(progress) + '%')
            if y[i] <= np.sin(x[i]):
                accepted_x.append(x[i])
                j += 1
    elif algorithm is "vectorise":
        accepted_x = np.extract((y <= np.sin(x)), x)

    return accepted_x

def runtime(func, runs, max_iter, algorithm=None):
    time = []
    for i in range(runs):
        start = timer()
        func()
        end = timer()
        time.append((end-start))
    error = np.std(time)
    time = sum(time)/runs

    return time, error

def time_analysis():
    time1, time2, time3, time4 = [], [], [], []
    error1, error2, error3, error4 = [], [], [], []
    for i in np.arange(1, 8, 1):
        max_iter = 10**i
        time, error = runtime(analytical_sine, 5, int(max_iter))
        time1.append(time)
        error1.append(error)
        time, error = runtime(analytical_sine, 5, int(max_iter), "vectorise")
        time2.append(time)
        error2.append(error)
        time, error = runtime(acc_rej_sine, 5, int(max_iter))
        time3.append(time)
        error3.append(error)
        time, error = runtime(acc_rej_sine(max_iter), 5, int(max_iter), "vectorise")
        time4.append(time)
        error4.append(error)
    return [time1, time2, time3, time4], [error1, error2, error3, error4]

# to run the code I would probably do something like this
time, error = time_analysis()
#then if I wanna plot number of iterations vs run time with errors I would probably do something along the lines of
plt.plot(max_iter, time) # max_iter would be a list of [10**i for i in np.arange(1, 8, 1)]
plt.errorbar(error)

因此,我的想法是我的runtime()函数将允许我放入要比较的4个函数中的任何一个(当前仍无法正常工作,我无法/无法弄清楚为什么),并运行5次,计算出平均运行时间和标准偏差作为误差。作为回报,我的time_analysis()函数将针对不同的runtime()(最大迭代次数)的不同函数运行max_iter,就像[10, 1E2, 1E3, 1E4, 1E5, 1E6, 1E7]一样,因此我可以针对运行时间绘制最大迭代次数。但是,由于我的time_analysis()要求我反复计算时间和错误并将其附加到列表中,因此整个方法似乎非常麻烦且笨拙。有没有更好的计时方法?(此外,我的runtime()仍然无法正常工作,因为我的algorithm参数似乎在使某些事情callable无效)

0 个答案:

没有答案