Python cProfile说,它应该更快;它的速度更快,但程序实际上运行得更慢

Python cProfile说,它应该更快;它的速度更快,但程序实际上运行得更慢,python,numpy,python-3.3,Python,Numpy,Python 3.3,希望你能帮我,因为这件事让我很头疼 我正在用python(3.3)开发一个小型捕食者-猎物模拟,它使用一个简单的前馈神经网络。今天,我将执行大脑每个“滴答”的函数从纯python更改为numpy数组,以便在我使用更大的大脑时对其进行优化 我用cProfile检查了整个主程序“循环”的运行速度,正如我所料,“滴答”功能(在大脑类中)更快。然而,在运行程序时,我注意到,实际上,使用numpy的速度较慢(12秒内100个循环,9秒内100个循环) 为什么会这样?代码如下: 最初的实施: class B

希望你能帮我,因为这件事让我很头疼

我正在用python(3.3)开发一个小型捕食者-猎物模拟,它使用一个简单的前馈神经网络。今天,我将执行大脑每个“滴答”的函数从纯python更改为numpy数组,以便在我使用更大的大脑时对其进行优化

我用cProfile检查了整个主程序“循环”的运行速度,正如我所料,“滴答”功能(在大脑类中)更快。然而,在运行程序时,我注意到,实际上,使用numpy的速度较慢(12秒内100个循环,9秒内100个循环)

为什么会这样?代码如下:

最初的实施:

class Brain:

def __init__(self, inputs, hidden, outputs):
    self.input_num = inputs
    self.hidden_num = hidden
    self.output_num = outputs

    self.h_weight = []
    self.o_weight = []
    for _ in range(self.input_num * self.hidden_num):
        self.h_weight.append(random.random()*2-1)
    for _ in range(self.hidden_num * self.output_num):
        self.o_weight.append(random.random()*2-1)

def tick(self):
    input_num = self.input_num 
    hidden_num = self.hidden_num
    output_num = self.output_num

    hidden = [0]*hidden_num
    output = [0]*output_num

    inputs = self.input
    h_weight = self.h_weight
    o_weight = self.o_weight

    e = math.e

    count = -1
    for x in range(hidden_num):
        temp = 0
        for y in range(input_num):
            count += 1
            temp -= inputs[y] * h_weight[count]
        hidden[x] = 1/(1+e**(temp))  

    count = -1      
    for x in range(output_num):
        temp = 0 
        for y in range(hidden_num):
            count += 1 
            temp -= hidden[y] * o_weight[count]
        output[x] = 1/(1+e**(temp))  

    self.output = output
新实现(使用numpy):

这是程序的主循环,我已经计时了(忽略其他函数,它们对“brain.tick()”函数没有影响)。它在另一个类中,这也是不相关的:

    def update(self):
    GUI.update()

    if not self.pause:   
        self.tick += 1

        if self.tick % 1000 == 0:
            if self.globalSelection:
                self.newGeneration(self.creatures)
            else:
                for specie in self.species:
                    if not specie.isPlant:
                        creatureList = [creature for creature in self.creatures if                creature.specie == specie] 
                        self.newGeneration(creatureList)   


        for creature in self.creatures:
            if not creature.specie.isPlant:
                if self.useHunger and creature.hunger < 1:
                    creature.hunger += 1/240    
                creature.setInputs()
                creature.brain.tick()
                creature.move(creature.brain.output[0]*50-25, creature.brain.output[1]*8)
                creature.interactions()

    threading.Timer(self.tickrate, self.update).start()
def更新(自):
GUI.update()
如果不是自我暂停:
self.tick+=1
如果self.tick%1000==0:
如果self.globalSelection:
自我新一代(自我生物)
其他:
对于自身物种中的物种:
如果不是specie.isPlant:
creatureList=[如果Bioture.specie==物种,则self.Bioties中的生物对应生物]
self.newGeneration(creatureList)
对于自身中的生物。生物:
如果不是bioter.specie.isPlant:
如果自用饥饿和生物饥饿<1:
生物。饥饿+=1/240
creative.setInputs()
生物。大脑。滴答声()
生物。移动(生物。大脑。输出[0]*50-25,生物。大脑。输出[1]*8)
生物相互作用()
threading.Timer(self.tickrate、self.update).start()
现在大脑被设置为5个输入,200个隐藏,2个输出,只是为了测试速度。 以下是cProfile的结果:

原件:

         3312 function calls in 0.094 seconds



Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.094    0.094 <string>:1(<module>)
      200    0.006    0.000    0.007    0.000 __init__.py:263(move)
      200    0.024    0.000    0.024    0.000 __init__.py:286(setInputs)
      200    0.001    0.000    0.001    0.000 __init__.py:360(interactions)
      200    0.033    0.000    0.033    0.000 __init__.py:415(tick)
        1    0.005    0.005    0.094    0.094 __init__.py:46(update)
        1    0.007    0.007    0.020    0.020 __init__.py:471(update)
        1    0.000    0.000    0.000    0.000 _weakrefset.py:79(add)
        3    0.000    0.000    0.000    0.000 threading.py:127(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:160(_release_save)
        1    0.000    0.000    0.000    0.000 threading.py:163(_acquire_restore)
        1    0.000    0.000    0.000    0.000 threading.py:166(_is_owned)
        1    0.000    0.000    0.000    0.000 threading.py:175(wait)
        2    0.000    0.000    0.000    0.000 threading.py:297(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:305(is_set)
        1    0.000    0.000    0.000    0.000 threading.py:325(wait)
        1    0.000    0.000    0.000    0.000 threading.py:507(_newname)
        1    0.000    0.000    0.000    0.000 threading.py:534(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:577(start)
        1    0.000    0.000    0.000    0.000 threading.py:775(daemon)
        1    0.000    0.000    0.000    0.000 threading.py:810(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:887(current_thread)
      225    0.002    0.000    0.002    0.000 {built-in method aacircle}
      200    0.001    0.000    0.001    0.000 {built-in method aaline}
      200    0.000    0.000    0.000    0.000 {built-in method abs}
        4    0.000    0.000    0.000    0.000 {built-in method allocate_lock}
      200    0.001    0.000    0.001    0.000 {built-in method atan2}
      400    0.001    0.000    0.001    0.000 {built-in method cos}
        1    0.000    0.000    0.094    0.094 {built-in method exec}
      225    0.001    0.000    0.001    0.000 {built-in method filled_circle}
        4    0.000    0.000    0.000    0.000 {built-in method filled_polygon}
        1    0.000    0.000    0.000    0.000 {built-in method get_ident}
        1    0.000    0.000    0.000    0.000 {built-in method get_pressed}
        1    0.000    0.000    0.000    0.000 {built-in method get}
        2    0.000    0.000    0.000    0.000 {built-in method len}
      200    0.000    0.000    0.000    0.000 {built-in method radians}
        1    0.000    0.000    0.000    0.000 {built-in method round}
      400    0.001    0.000    0.001    0.000 {built-in method sin}
        1    0.000    0.000    0.000    0.000 {built-in method start_new_thread}
        1    0.006    0.006    0.006    0.006 {built-in method update}
        5    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}
        1    0.000    0.000    0.000    0.000 {method 'add' of 'set' objects}
        1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        2    0.000    0.000    0.000    0.000 {method 'blit' of 'pygame.Surface' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.001    0.001    0.001    0.001 {method 'fill' of 'pygame.Surface' objects}
        8    0.000    0.000    0.000    0.000 {method 'random_sample' of 'mtrand.RandomState' objects}
        2    0.000    0.000    0.000    0.000 {method 'release' of '_thread.lock' objects}
        3    0.000    0.000    0.000    0.000 {method 'render' of 'pygame.font.Font' objects}
0.094秒内3312次函数调用
订购人:标准名称
ncalls tottime percall cumtime percall文件名:lineno(函数)
1    0.000    0.000    0.094    0.094 :1()
200 0.006 0.000 0.007 0.000________.py:263(移动)
200 0.024 0.000 0.024 0.000________.py:286(设置输入)
200 0.001 0.000 0.001 0.000 uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu初始值:360(交互作用)
200 0.033 0.000 0.033 0.000初始值:415(勾选)
1 0.005 0.005 0.094 0.094初始值:46(更新)
1 0.007 0.007 0.020 0.020初始值:471(更新)
1 0.000 0.000 0.000 0.000 _weakrefset.py:79(新增)
3 0.000 0.000 0.000 0.000螺纹。py:127(初始)
1 0.000 0.000 0.000 0.000螺纹。py:160(_release_save)
1 0.000 0.000 0.000 0.000线程。py:163(\u获取\u恢复)
1 0.000 0.000 0.000 0.000螺纹。py:166(_为_所有)
1 0.000 0.000 0.000 0.000螺纹。py:175(等待)
2 0.000 0.000 0.000 0.000螺纹。py:297(初始)
1 0.000 0.000 0.000 0.000螺纹。py:305(成套)
1 0.000 0.000 0.000 0.000螺纹。py:325(等待)
1 0.000 0.000 0.000 0.000螺纹。py:507(_newname)
1 0.000 0.000 0.000 0.000螺纹。py:534(初始)
1 0.000 0.000 0.000 0.000螺纹。py:577(开始)
1 0.000 0.000 0.000 0.000线程。py:775(守护进程)
1 0.000 0.000 0.000 0.000螺纹。py:810(初始)
1 0.000 0.000 0.000 0.000螺纹。py:887(当前螺纹)
225 0.002 0.000 0.002 0.000{内置方法aacircle}
200 0.001 0.000 0.001 0.000{内置方法aaline}
200 0.000 0.000 0.000 0.000{内置方法abs}
4 0.000 0.000 0.000 0.000{内置方法分配锁}
200 0.001 0.000 0.001 0.000{内置方法atan2}
400 0.001 0.000 0.001 0.000{内置方法cos}
1 0.000 0.000 0.094 0.094{内置方法exec}
225 0.001 0.000 0.001 0.000{内置方法填充_圆}
4 0.000 0.000 0.000 0.000{内置方法填充_多边形}
1 0.000 0.000 0.000 0.000{内置方法get_ident}
1 0.000 0.000 0.000 0.000{内置方法get_pressed}
1 0.000 0.000 0.000 0.000{内置方法get}
2 0.000 0.000 0.000 0.000{内置方法len}
200 0.000 0.000 0.000 0.000{内置方法弧度}
1 0.000 0.000 0.000 0.000{内置方法圆形}
400 0.001 0.000 0.001 0.000{内置方法sin}
1 0.000 0.000 0.000 0.000{内置方法开始\新线程}
1 0.006 0.006 0.006 0.006{内置方法更新}
5 0.000 0.000 0.000 0.000{method'acquire'of'\u thread.lock'objects}
1 0.000 0.000 0.000 0.000{“set”对象的“add”方法}
1 0.000 0.000 0.000 0.000{“列表”对象的“附加”方法}
2 0.000 0.000 0.000 0.000{“pygame.Surface”对象的方法“blit”}
1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}
1 0.001 0.001 0.001 0.001{“pygame.Surface”对象的“填充”方法}
8 0.000 0.000 0.000 0.000{“mtrand.RandomState”对象的方法“随机样本”}
2 0.000 0.000 0.000 0.000{方法'release'的'\u thread.lock'对象}
3 0.000 0.000 0.000 0.000{“pygame.font.F”的“render”方法
         3312 function calls in 0.094 seconds



Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.094    0.094 <string>:1(<module>)
      200    0.006    0.000    0.007    0.000 __init__.py:263(move)
      200    0.024    0.000    0.024    0.000 __init__.py:286(setInputs)
      200    0.001    0.000    0.001    0.000 __init__.py:360(interactions)
      200    0.033    0.000    0.033    0.000 __init__.py:415(tick)
        1    0.005    0.005    0.094    0.094 __init__.py:46(update)
        1    0.007    0.007    0.020    0.020 __init__.py:471(update)
        1    0.000    0.000    0.000    0.000 _weakrefset.py:79(add)
        3    0.000    0.000    0.000    0.000 threading.py:127(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:160(_release_save)
        1    0.000    0.000    0.000    0.000 threading.py:163(_acquire_restore)
        1    0.000    0.000    0.000    0.000 threading.py:166(_is_owned)
        1    0.000    0.000    0.000    0.000 threading.py:175(wait)
        2    0.000    0.000    0.000    0.000 threading.py:297(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:305(is_set)
        1    0.000    0.000    0.000    0.000 threading.py:325(wait)
        1    0.000    0.000    0.000    0.000 threading.py:507(_newname)
        1    0.000    0.000    0.000    0.000 threading.py:534(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:577(start)
        1    0.000    0.000    0.000    0.000 threading.py:775(daemon)
        1    0.000    0.000    0.000    0.000 threading.py:810(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:887(current_thread)
      225    0.002    0.000    0.002    0.000 {built-in method aacircle}
      200    0.001    0.000    0.001    0.000 {built-in method aaline}
      200    0.000    0.000    0.000    0.000 {built-in method abs}
        4    0.000    0.000    0.000    0.000 {built-in method allocate_lock}
      200    0.001    0.000    0.001    0.000 {built-in method atan2}
      400    0.001    0.000    0.001    0.000 {built-in method cos}
        1    0.000    0.000    0.094    0.094 {built-in method exec}
      225    0.001    0.000    0.001    0.000 {built-in method filled_circle}
        4    0.000    0.000    0.000    0.000 {built-in method filled_polygon}
        1    0.000    0.000    0.000    0.000 {built-in method get_ident}
        1    0.000    0.000    0.000    0.000 {built-in method get_pressed}
        1    0.000    0.000    0.000    0.000 {built-in method get}
        2    0.000    0.000    0.000    0.000 {built-in method len}
      200    0.000    0.000    0.000    0.000 {built-in method radians}
        1    0.000    0.000    0.000    0.000 {built-in method round}
      400    0.001    0.000    0.001    0.000 {built-in method sin}
        1    0.000    0.000    0.000    0.000 {built-in method start_new_thread}
        1    0.006    0.006    0.006    0.006 {built-in method update}
        5    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}
        1    0.000    0.000    0.000    0.000 {method 'add' of 'set' objects}
        1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        2    0.000    0.000    0.000    0.000 {method 'blit' of 'pygame.Surface' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.001    0.001    0.001    0.001 {method 'fill' of 'pygame.Surface' objects}
        8    0.000    0.000    0.000    0.000 {method 'random_sample' of 'mtrand.RandomState' objects}
        2    0.000    0.000    0.000    0.000 {method 'release' of '_thread.lock' objects}
        3    0.000    0.000    0.000    0.000 {method 'render' of 'pygame.font.Font' objects}
                3322 function calls in 0.068 seconds



 Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.068    0.068 <string>:1(<module>)
      200    0.005    0.000    0.006    0.000 __init__.py:265(move)
      200    0.022    0.000    0.023    0.000 __init__.py:288(setInputs)
      200    0.001    0.000    0.001    0.000 __init__.py:362(interactions)
      200    0.005    0.000    0.014    0.000 __init__.py:417(tick)
        1    0.005    0.005    0.068    0.068 __init__.py:47(update)
        1    0.005    0.005    0.019    0.019 __init__.py:473(update)
        1    0.000    0.000    0.000    0.000 _weakrefset.py:79(add)
        3    0.000    0.000    0.000    0.000 threading.py:127(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:160(_release_save)
        1    0.000    0.000    0.000    0.000 threading.py:163(_acquire_restore)
        1    0.000    0.000    0.000    0.000 threading.py:166(_is_owned)
        1    0.000    0.000    0.000    0.000 threading.py:175(wait)
        2    0.000    0.000    0.000    0.000 threading.py:297(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:305(is_set)
        1    0.000    0.000    0.000    0.000 threading.py:325(wait)
        1    0.000    0.000    0.000    0.000 threading.py:507(_newname)
        1    0.000    0.000    0.000    0.000 threading.py:534(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:577(start)
        1    0.000    0.000    0.000    0.000 threading.py:775(daemon)
        1    0.000    0.000    0.000    0.000 threading.py:810(__init__)
        1    0.000    0.000    0.000    0.000 threading.py:887(current_thread)
      225    0.002    0.000    0.002    0.000 {built-in method aacircle}
      200    0.001    0.000    0.001    0.000 {built-in method aaline}
      200    0.001    0.000    0.001    0.000 {built-in method abs}
        4    0.000    0.000    0.000    0.000 {built-in method allocate_lock}
      200    0.000    0.000    0.000    0.000 {built-in method atan2}
      400    0.001    0.000    0.001    0.000 {built-in method cos}
      400    0.008    0.000    0.008    0.000 {built-in method dot}
        1    0.000    0.000    0.068    0.068 {built-in method exec}
      225    0.001    0.000    0.001    0.000 {built-in method filled_circle}
        4    0.000    0.000    0.000    0.000 {built-in method filled_polygon}
        1    0.000    0.000    0.000    0.000 {built-in method get_ident}
        1    0.000    0.000    0.000    0.000 {built-in method get_pressed}
        1    0.000    0.000    0.000    0.000 {built-in method get}
        2    0.000    0.000    0.000    0.000 {built-in method len}
      200    0.000    0.000    0.000    0.000 {built-in method radians}
        1    0.000    0.000    0.000    0.000 {built-in method round}
      400    0.001    0.000    0.001    0.000 {built-in method sin}
        1    0.000    0.000    0.000    0.000 {built-in method start_new_thread}
        1    0.005    0.005    0.005    0.005 {built-in method update}
        5    0.000    0.000    0.000    0.000 {method 'acquire' of '_thread.lock' objects}
        1    0.000    0.000    0.000    0.000 {method 'add' of 'set' objects}
        1    0.000    0.000    0.000    0.000 {method 'append' of 'list' objects}
        2    0.000    0.000    0.000    0.000 {method 'blit' of 'pygame.Surface' objects}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.002    0.002    0.002    0.002 {method 'fill' of 'pygame.Surface' objects}
       18    0.000    0.000    0.000    0.000 {method 'random_sample' of 'mtrand.RandomState' objects}
        2    0.000    0.000    0.000    0.000 {method 'release' of '_thread.lock' objects}
        3    0.000    0.000    0.000    0.000 {method 'render' of 'pygame.font.Font' objects}
self.h_activation = zeros((self.hidden_num, 1), dtype=float)
self.o_activation = zeros((self.output_num, 1), dtype=float)

self.i_output = zeros((self.input_num, 1), dtype=float)      
self.h_output = zeros((self.hidden_num, 1), dtype=float)  
self.o_output = zeros((self.output_num, 1), dtype=float)