Python:如何在赋值过程中保留变量内存位置?

发布于 2024-10-21 09:53:24 字数 441 浏览 4 评论 0原文

我对 Python 还很陌生,所以如果这个问题很愚蠢,请原谅我。我无法通过Google找到答案...

我在我的代码中使用PyFFTW,它有一个规划阶段,您可以在其中传递两个变量(源/目标),即您的转换来源/目标。然后,当您调用 FFT 时,它将对这些变量在规划阶段所处的确切内存空间进行操作。因此,需要对变量进行任何操作,以便这两个变量在内存中的位置不会改变。 我找到了运算符 *=+= 等,它们对标准数学运算符执行此操作。 然而,在我的程序中,我需要对变量应用一个函数,这应该将其返回到相同的内存位置。 这该怎么办?!

我最初按以下方式使用切片:

a[:] = func(a)[:]

但是我刚刚意识到,这非常慢(我的代码大约慢了 10%)。 那么有人知道该怎么做吗?

非常感谢任何帮助。提前致谢!

I'm still pretty new to Python, so forgive me this question if it's stupid. I could not find an answer through Google...

I am using PyFFTW in my code, which has a planning stage, where you pass it the two variables (source/destination), that your transforming from/to. It will then, when you call the FFT operate on the exact memory space, where those variables were located during the planning stage. Thus any operations done on the variables will need to be done, so that the location in memory of these two variables do not change.
I found the operators *=, +=, etc., which do this for standard mathematical operators.
However in my program I need to apply a function to the variable, which should return it to the same memory location.
How to do this?!

I initially used slicing in the following way:

a[:] = func(a)[:]

However I just realized, that this is extremely slow (my code is about 10% slower).
So does anybody know how to go about this?

Any help is very appreciated. Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

暮年 2024-10-28 09:53:24

您的变量是可变类型,因此您的函数可以直接对其进行操作。

您仍然无法使用将要创建副本和/或进行新分配的函数和运算符(与您已经无法使用的相同),但是函数参数的直接突变将在外部可见功能。

Your variable is a mutable type, so your function can just operate on it directly.

You still won't be able to use functions and operators that are going to create copies and/or make new assignments (the same ones you couldn't use already), but direct mutations of the argument of your function will be visible outside the function.

寂寞花火° 2024-10-28 09:53:24

如何使用本地值并将其引用到全局值。我想它可以做得更快...

global a
a = []
def test(): 
    global a
    b = [1,2,3,4]
    a = b
....

嗯..我还必须用 cProfiler 测试它

how about using local value and referring it to the global value. I guess it could make faster...

global a
a = []
def test(): 
    global a
    b = [1,2,3,4]
    a = b
....

um.. I also would have to test it with cProfiler

℉絮湮 2024-10-28 09:53:24

这是你想做的事情吗?

def time10(a):
  """ multiple the elements by 10 in-place """
  for i in xrange(len(a)):
    a[i] *= 10
  # returning a is optional. I think it maybe useful for use to chain the operation
  return a

>>> a = range(10)
>>> time10(a)
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> a
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> b = range(10)
>>> time10(time10(b))
[0, 100, 200, 300, 400, 500, 600, 700, 800, 900]
>>> b
[0, 100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>

您的原始代码在返回后复制数组。这通常不是一个有用的做法,并且会导致运行时间变慢。

Is this something you are trying to do?

def time10(a):
  """ multiple the elements by 10 in-place """
  for i in xrange(len(a)):
    a[i] *= 10
  # returning a is optional. I think it maybe useful for use to chain the operation
  return a

>>> a = range(10)
>>> time10(a)
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> a
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
>>> b = range(10)
>>> time10(time10(b))
[0, 100, 200, 300, 400, 500, 600, 700, 800, 900]
>>> b
[0, 100, 200, 300, 400, 500, 600, 700, 800, 900]
>>>

Your original code copy the array after it returns. It is usually not a useful practice and contribute to your slower runtime.

佞臣 2024-10-28 09:53:24

所以最终我也没有找到满意的解决方案。
我最初最终使用了上面 delnan 提出的解决方案

a[:] = func(a)[:]

并预分配了一定大小的数组。
很抱歉将此添加为我的答案,因为我不知道如何/是否可以接受 delnans 评论作为答案......

So in the end I could not find a satisfying solution to the problem.
I initially ended up using the solution proposed by delnan above

a[:] = func(a)[:]

and preallocating the arrays of a certain size.
Sorry for adding this as my answer, as I couldn't figure out how to/if it's possible accept delnans comments as the answer...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文