相关疑难解决方法(0)

测量Python中经过的时间?

我想要的是开始在我的代码中的某个地方计算时间,然后获得通过的时间,以测量执行少量功能所花费的时间.我认为我使用的是timeit模块错误,但文档对我来说只是让人困惑.

import timeit

start = timeit.timeit()
print("hello")
end = timeit.timeit()
print(end - start)
Run Code Online (Sandbox Code Playgroud)

python performance timeit measure

1031
推荐指数
32
解决办法
116万
查看次数

使用timeit时如何传递函数的参数.Timer()

这是一个简单程序的概述

# some pre-defined constants
A = 1
B = 2

# function that does something critical
def foo(num1, num2):
    # do something

# main program.... do something to A and B
for i in range(20):
    # do something to A and B
    # and update A and B during each iteration

import timeit
t = timeit.Timer(stmt="foo(num1,num2)")  
print t.timeit(5)
Run Code Online (Sandbox Code Playgroud)

我只是不断得到"全球名称foo没有定义".....任何人都可以帮助我吗?谢谢!

python timer

41
推荐指数
7
解决办法
3万
查看次数

在Python中同时执行两个类方法

我相信之前已经提出过许多类似的问题,但在阅读了很多类似的问题后,我仍然不太确定应该做些什么.所以,我有一个Python脚本来控制一些外部仪器(摄像头和功率计).我通过使用ctypes调用.dll文件中的C函数为两种乐器编写了类.现在它看起来像这样:

for i in range(10):
    power_reading = newport.get_reading(N=100,interval=1) # take power meter reading
    img = camera.capture(N=10)
    value = image_processing(img) # analyze the img (ndarray) to get some values
    results.append([power_reading,value]) # add both results to a list
Run Code Online (Sandbox Code Playgroud)

我想同时开始执行前两行.双方newport.get_readingcamera.capture需要100ms-1运行(他们会在同一时间运行,如果我选择了正确的参数).我不需要它们在同一时间完全启动,但理想情况下延迟应该小于总运行时间的大约10-20%(因此当每次运行时运行时间小于0.2秒).根据我的阅读,我可以使用该multiprocessing模块.所以我根据这篇文章尝试这样的事情:

def p_get_reading(newport,N,interval,return_dict):
    reading = newport.get_reading(N,interval,return_dict)
    return_dict['power_meter'] = reading

def p_capture(camera,N,return_dict):
    img = camera.capture(N)
    return_dict['image'] = img

for i in range(10):
    manager = multiprocessing.Manager()
    return_dict = manager.dict()
    p = multiprocessing.Process(target=p_capture, args=(camera,10))
    p.start()
    p2 = multiprocessing.Process(target=p_get_reading, args=(newport,100,1)) …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing python-multithreading python-3.x python-multiprocessing

9
推荐指数
1
解决办法
1643
查看次数

设置封面或击球套装; Numpy,最少的元素组合,以弥补全套

我的目标是找到尽可能少的子集[af]来组成全集A.

A = set([1,2,3,4,5,6,7,8,9,10]) # full set


#--- below are sub sets of A ---

a = set([1,2])
b = set([1,2,3])
c = set([1,2,3,4])
d = set([4,5,6,7])
e = set([7,8,9])
f = set([5,8,9,10])
Run Code Online (Sandbox Code Playgroud)

实际上,我正在处理的父集A包含15k个独特元素,具有30k个子集,这些子集的长度范围从单个唯一元素到1.5k个唯一元素.

截至目前,我正在使用的代码看起来或多或少像以下一样,并且很慢:

import random


B = {'a': a, 'b': b, 'c': c, 'd': d, 'e': e, 'f': f}
Bx = B.keys()
random.shuffle(Bx)

Dict = {}

for i in Bx: # iterate through shuffled keys.
    z = [i]
    x = B[i]
    L = len(x)

    while L < len(A): …
Run Code Online (Sandbox Code Playgroud)

python algorithm combinations numpy set

7
推荐指数
1
解决办法
1705
查看次数

加快在matplotlib中绘制图像的速度

我在Spyder IDE中编写了一些Python来并排绘制一对图像,以便我可以直观地检查它们.我大多数时间只需要3秒钟来观察它们,但每隔一段时间我需要更长时间才能仔细观察.因此,我没有使用time.sleep,而是将其编码为等待我按下Enter键,如下所示:

import matplotlib.pyplot as plt
import os

def VI_segmentation():
    root = os.getcwd()
    NR_dir = root + '\\Neurite_Results\\'
    SO_dir = root + '\\Segmentation_Overlays\\'
    jpgs = os.listdir(NR_dir)
    fig = plt.figure(figsize=(20,12))
    for jpg in jpgs:
        fig.suptitle(jpg , fontsize=14, fontweight='bold')
        image_NR = plt.imread(NR_dir + jpg)
        image_SO = plt.imread(SO_dir + jpg)
        plt.subplot(121)
        plt.imshow(image_NR)
        plt.subplot(122)
        plt.imshow(image_SO)
        plt.draw()
        plt.pause(0.01)

        input('Press Enter to continue')

VI_segmentation()
Run Code Online (Sandbox Code Playgroud)

问题是我的思维速度比我的计算机快:).计算机响应Enter键需要5或6秒钟,响应后需要几秒钟才能更新.通过数百张大多数精细的图像来制作糟糕的人体工程学设计.任何简化此代码的想法都将非常感激.

python matplotlib spyder

2
推荐指数
1
解决办法
7504
查看次数