numpy:点和
>>> from pandac.PandaModules import Vec3
>>> import numpy
>>> l = []
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> Vec3(1,1,1)+Vec3(1,1,1)
Vec3(2, 2, 2)
>>> sum(l)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'libpanda.Vec3'
>>> numpy.sum(l)
9.0
>>>
我想要一些快速(快速==不是纯Python中的循环,而是numpy速度)方法来实现:
>>> my_smart_sum(l)
Vec3(3,3,3)
>>> from pandac.PandaModules import Vec3
>>> import numpy
>>> l = []
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> l.append( Vec3(1,1,1) )
>>> Vec3(1,1,1)+Vec3(1,1,1)
Vec3(2, 2, 2)
>>> sum(l)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'libpanda.Vec3'
>>> numpy.sum(l)
9.0
>>>
i want some fast (fast == not for loop in pure python but numpy velocity) method to achive:
>>> my_smart_sum(l)
Vec3(3,3,3)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
或者,使用 numpy,这个:
速度取决于向量加法的实现。您应该使用
timeit
来确定哪种方法最快。这可能看起来像这样:您传递的第一个字符串是设置语句 - 这不会包含在计时中。您传递的第二个字符串是您实际想要计时的代码。我对
pandac
一无所知,但有时使用 Cython 可以大大加快数字运算循环的速度。Try this:
Or, with numpy, this:
The speed depends in the implementation of the vector-addition. You should use
timeit
to determine which method is fastest. This could look something like this:The first string you pass is the setup-statement - this will not be included in the timing. The second string you pass is the code you actually want to time. I don't know anything about
pandac
, but sometimes number-crunching loops can be sped up a lot using Cython.