3

interact()在 Jupyter 笔记本中使用绘制数据的小部件创建了一个小部件(参见下面的代码)。但是,当我移动滑块时,小部件更新非常缓慢(大约 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));
4

0 回答 0