实际上是 Sublime Text 的菜鸟,但我在尝试用它运行 Python 时遇到了障碍,当我尝试用它构建时,似乎没有任何东西运行或绘制

问题描述 投票:0回答:0

所以当我尝试运行/构建代码时,它卡在了阶段并且没有错误显示但是代码没有发生任何事情所以我试图调试它并尝试让它绘制这样的图表,

这是我试图运行的代码。

import math
import numpy as np
import time
import multiprocessing as mp
import matplotlib.pyplot as plt

def f(x):
    return math.acos(math.cos(x) / (1 + 2 * math.cos(x)))

b = math.pi / 2
a = 0
exact = math.pi ** 2 / 16
h = (b - a)

def g(j):
    global a
    global h
    return (f(a + (2 * j - 2) * h) + 4 * f(a + (2 * j - 1) * h) + f(a + (2 * j) * h))

if __name__ == '__main__':
    mp.freeze_support()  #needed for Windows

    pool = mp.Pool(processes=mp.cpu_count())

    serial_times = []
    parallel_times = []
    intervals = []

    for i in range(1, 10):
        n = 10 ** i
        h = (b - a) / n
        N = int(n / 2)
        intervals.append(N)
    
        start_time = time.time()
        p = pool.map(g, np.linspace(1, N, N, dtype=np.int32))
        P = (h / 3) * np.sum(p, dtype=np.float32)
        elapsedTime = time.time() - start_time
        parallel_times.append(elapsedTime)

        start_time = time.time()
        S = 0
        for j in range(1, N+1):
            S += f(a + (2 * j - 2) * h) + 4 * f(a + (2 * j - 1) * h) + f(a + (2 * j) * h)
        S *= h / 3
        elapsedTime = time.time() - start_time
        serial_times.append(elapsedTime)

    #Plotting
    plt.plot(intervals, serial_times, label='Serial Computation')
    plt.plot(intervals, parallel_times, label='Parallel Computation')
    plt.xlabel('Interval Count (N)')
    plt.ylabel('Runtime (s)')
    plt.xscale('log')
    plt.yscale('log')
    plt.title('Simpson\'s Rule Computation Time vs Interval Count')
    plt.legend()
    plt.show()
python python-3.x sublimetext3 sublime-text-plugin
© www.soinside.com 2019 - 2024. All rights reserved.