纯Python比Numpy快吗?我可以使这个Numpy代码更快吗? [英] Pure Python faster than Numpy? can I make this numpy code faster?

查看:196
本文介绍了纯Python比Numpy快吗?我可以使这个Numpy代码更快吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从特定的面/顶点列表中计算最小值,最大值和均值.我尝试使用Numpy优化此计算,但没有成功.

I need to compute the min, max, and mean from a specific list of faces/vertices. I tried to optimize this computing with the use of Numpy but without success.

这是我的测试用例:

#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
'''
Module Started 22 févr. 2013
@note: test case comparaison numpy vs python
@author: Python4D/damien
'''

import numpy as np
import time


def Fnumpy(vertices):
  np_vertices=np.array(vertices)
  _x=np_vertices[:,:,0]
  _y=np_vertices[:,:,1]
  _z=np_vertices[:,:,2]
  _min=[np.min(_x),np.min(_y),np.min(_z)]
  _max=[np.max(_x),np.max(_y),np.max(_z)]
  _mean=[np.mean(_x),np.mean(_y),np.mean(_z)]
  return _mean,_max,_min

def Fpython(vertices):
  list_x=[item[0] for sublist in vertices for item in sublist]
  list_y=[item[1] for sublist in vertices for item in sublist]
  list_z=[item[2] for sublist in vertices for item in sublist]
  taille=len(list_x)
  _mean=[sum(list_x)/taille,sum(list_y)/taille,sum(list_z)/taille]
  _max=[max(list_x),max(list_y),max(list_z)]
  _min=[min(list_x),min(list_y),min(list_z)]    
  return _mean,_max,_min

if __name__=="__main__":
  vertices=[[[1.1,2.2,3.3,4.4]]*4]*1000000
  _t=time.clock()
  print ">>NUMPY >>{} for {}s.".format(Fnumpy(vertices),time.clock()-_t)
  _t=time.clock()
  print ">>PYTHON>>{} for {}s.".format(Fpython(vertices),time.clock()-_t)

结果是:

脾气暴躁:

([1.1000000000452519、2.2000000000905038、3.3000000001880174],[1.1000000000000001、2.2000000000000002、3.2999999999999998,[1.1000000000000001、2.2000000000000002、3.2999999999999998]) 27.327068618s.

Python:

([1.100000000045252、2.200000000090504、3.3000000001880174,[1.1、2.2、3.3],[1.1、2.2、3.3]) 1.81366938593s.

纯Python比Numpy快15倍!

Pure Python is 15x faster than Numpy!

推荐答案

Fnumpy较慢的原因是它包含Fpython未完成的附加步骤:在内存中创建一个numpy数组.如果将行np_verticies=np.array(verticies)移到Fnumpy之外,并且将定时部分移到定时部分,则结果将大不相同:

The reason your Fnumpy is slower is that it contains an additional step not done by Fpython: the creation of a numpy array in memory. If you move the line np_verticies=np.array(verticies) outside of Fnumpy and the timed section your results will be very different:

>>NUMPY >>([1.1000000000452519, 2.2000000000905038, 3.3000000001880174], [1.1000000000000001, 2.2000000000000002, 3.2999999999999998], [1.1000000000000001, 2.2000000000000002, 3.2999999999999998]) for 0.500802s.
>>PYTHON>>([1.100000000045252, 2.200000000090504, 3.3000000001880174], [1.1, 2.2, 3.3], [1.1, 2.2, 3.3]) for 2.182239s.

您还可以通过在创建numpy时为numpy提供数据类型提示来显着加快分配步骤.如果您告诉Numpy您有一个浮点数组,那么即使您在计时循环中留下np.array()调用,它也会击败纯python版本.

You can also speed up the allocation step significantly by providing a datatype hint to numpy when you create it. If you tell Numpy you have an array of floats, then even if you leave the np.array() call in the timing loop it will beat the pure python version.

如果我将np_vertices=np.array(vertices)更改为np_vertices=np.array(vertices, dtype=np.float_)并将其保留在Fnumpy 中,则Fnumpy版本将击败Fpython,即使它需要做更多的工作:

If I change np_vertices=np.array(vertices) to np_vertices=np.array(vertices, dtype=np.float_) and keep it in Fnumpy, the Fnumpy version will beat Fpython even though it has to do a lot more work:

>>NUMPY >>([1.1000000000452519, 2.2000000000905038, 3.3000000001880174], [1.1000000000000001, 2.2000000000000002, 3.2999999999999998], [1.1000000000000001, 2.2000000000000002, 3.2999999999999998]) for 1.586066s.
>>PYTHON>>([1.100000000045252, 2.200000000090504, 3.3000000001880174], [1.1, 2.2, 3.3], [1.1, 2.2, 3.3]) for 2.196787s.

这篇关于纯Python比Numpy快吗?我可以使这个Numpy代码更快吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆