如何加快IPython小部件的执行速度?

时间:2016-01-27 06:08:24

标签: python matplotlib ipywidgets

我在Jupyter笔记本中使用interact()创建了一个小部件,用于绘制数据(参见下面的代码)。但是,当我移动滑块时,小部件更新速度非常慢(大约1 fps)。

评估函数neg(T)pos(T)内的数据大约需要1e-4秒,并在函数conc(T)内绘制数据 - 大约1e-1秒。那为什么我的小部件有这么慢的响应呢?我怎么能改进它?

from scipy import constants
from numpy import *
import matplotlib.pyplot as plt
from ipywidgets import interact
import time

%matplotlib inline

m_e = m_h = 1.5 * 10**-49
hbar = constants.hbar
k = constants.k / constants.e

E_g = 1.1242
E_v = 0
E_c = E_v + E_g
E_a = E_v + 0.045
E_d = E_c - 0.045
n_a = 10**14
n_d = 10**12
g_a = 4
g_d = 2

E_f = linspace(E_v, E_c, 100)

def neg(T):
    start = time.time()

    N_c = 2 * 10**-6 * (m_e*k*T/(2*pi*hbar**2))**1.5
    n = N_c * exp((E_f-E_c)/(k*T))
    N_a_minus = n_a/(1+g_a*exp((E_a-E_f)/(k*T)))

    print(time.time()-start)

    return n + N_a_minus

def pos(T):
    start = time.time()

    N_v = 2 * 10**-6 * (m_h*k*T/(2*pi*hbar**2))**1.5
    p = N_v * exp((E_v-E_f)/(k*T))
    N_d_plus = n_d/(1+g_d*exp((E_f-E_d)/(k*T)))

    print(time.time()-start)

    return p + N_d_plus

def conc(T):
    start = time.time()

    plt.plot(E_f, neg(T), E_f, pos(T))
    plt.axis([E_v,E_g,10.0**6,10.0**21])
    plt.yscale('log')

    print(time.time()-start)

interact(conc, T=(50,800,50));

0 个答案:

没有答案