Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'pypy': 0.07; 'subject:code': 0.07; '3),': 0.09; 'alternatives': 0.09; 'input,': 0.09; 'python:': 0.09; 'subject:How': 0.10; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'anyways,': 0.16; 'c/c++': 0.16; 'cc:name:python list': 0.16; 'cheers!': 0.16; 'fabio': 0.16; 'increment': 0.16; 'inputs': 0.16; 'invocations': 0.16; 'numpy': 0.16; 'subject:make': 0.16; 'temp': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'bit': 0.19; 'pfxlen:0': 0.19; 'email addr:gmail.com>': 0.22; 'network,': 0.22; 'cc:addr:python.org': 0.22; '2.x': 0.24; 'algorithms.': 0.24; 'cheers,': 0.24; '(or': 0.24; 'question': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'quite': 0.32; 'url:python': 0.33; 'implemented': 0.33; 'maybe': 0.34; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'method': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'seconds': 0.37; 'level': 0.37; 'skip:& 10': 0.38; 'skip:[ 10': 0.38; 'pm,': 0.38; 'heard': 0.39; 'subject:can': 0.39; 'skip:x 10': 0.40; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'simple': 0.61; "you're": 0.61; 'save': 0.62; 'making': 0.63; 'more': 0.64; '(that': 0.65; 'to:addr:gmail.com': 0.65; 'within': 0.65; '20,': 0.68; 'anything.': 0.68; 'brain': 0.68; 'jul': 0.74; 'obvious': 0.74; 'subject:this': 0.83; 'low': 0.83; 'calls,': 0.84; 'faster.': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=sMP9ddbF2Nslc5ttvJmGaFsAgtJ+fV1Onf5K1Xcpq00=; b=gdQgtnIFWxEYplcWBA0HWyvgsrs4lZFjEJzV6HjepHlV/Fppt8AraaLRGZV/le7opF JQBjPHmtMDzH2ePYLeEXrMmGuMlm95+N3QJ7X3o7boOX++MR0vVfaIiUR8B7oiPJF6TB B9q7TujVwhPbFadK/EQ8HQ4lvJcwcZMhOjiW27Zg2YFVTCGl75sh96yawGIxA0Ob4z14 FlPwHCvEFFYgkKs0gSqyFoUrIb+neL+I+71beGSUx4kl5En+J6buc+Ud+8EwsDNaHcsH VWLUUSqJkL9ComV/Fg7nk3mnZvFZ/V6ior22C3JjgtU2TmmwLuo1IXfYDgOgiYvfIjdX TUng== X-Received: by 10.221.49.134 with SMTP id va6mr7554162vcb.14.1374354363956; Sat, 20 Jul 2013 14:06:03 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> References: <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> From: Fabio Zadrozny Date: Sat, 20 Jul 2013 18:05:43 -0300 Subject: Re: How can I make this piece of code even faster? To: pablobarhamalzas@gmail.com Content-Type: multipart/alternative; boundary=001a11339edaa8d67b04e1f7cfc7 Cc: python list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 152 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374354373 news.xs4all.nl 15926 [2001:888:2000:d::a6]:58392 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50975 --001a11339edaa8d67b04e1f7cfc7 Content-Type: text/plain; charset=ISO-8859-1 On Sat, Jul 20, 2013 at 5:22 PM, wrote: > Ok, I'm working on a predator/prey simulation, which evolve using genetic > algorithms. At the moment, they use a quite simple feed-forward neural > network, which can change size over time. Each brain "tick" is performed by > the following function (inside the Brain class): > > 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 > > The function is actually quite fast (~0.040 seconds per 200 calls, using > 10 input, 20 hidden and 3 output neurons), and used to be much slower > untill I fiddled about with it a bit to make it faster. However, it is > still somewhat slow for what I need it. > > My question to you is if you an see any obvious (or not so obvious) way of > making this faster. I've heard about numpy and have been reading about it, > but I really can't see how it could be implemented here. > > Cheers! > -- > http://mail.python.org/mailman/listinfo/python-list > Low level optimizations: If you're in Python 2.x (and not 3), you should use xrange() instead of range(), or maybe even create a local variable and increment it and check its value within a while (that way you can save a few instructions on method invocations from xrange/range). Anyways, if that's not fast enough, just port it to c/c++ (or one of the alternatives to speed it up while still in python: numba, cython, shedskin). Or (if you can), try to use PyPy and see if you get more speed without doing anything. Cheers, Fabio --001a11339edaa8d67b04e1f7cfc7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

= On Sat, Jul 20, 2013 at 5:22 PM, <pablobarhamalzas@gmail.com= > wrote:
Ok, I'm working on a = predator/prey simulation, which evolve using genetic algorithms. At the mom= ent, they use a quite simple feed-forward neural network, which can change = size over time. Each brain "tick" is performed by the following f= unction (inside the Brain class):

=A0 =A0 def tick(self):
=A0 =A0 =A0 =A0 input_num =3D self.input_num
=A0 =A0 =A0 =A0 hidden_num =3D self.hidden_num
=A0 =A0 =A0 =A0 output_num =3D self.output_num

=A0 =A0 =A0 =A0 hidden =3D [0]*hidden_num
=A0 =A0 =A0 =A0 output =3D [0]*output_num

=A0 =A0 =A0 =A0 inputs =3D self.input
=A0 =A0 =A0 =A0 h_weight =3D self.h_weight
=A0 =A0 =A0 =A0 o_weight =3D self.o_weight

=A0 =A0 =A0 =A0 e =3D math.e

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

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

=A0 =A0 =A0 =A0 self.output =3D output

The function is actually quite fast (~0.040 seconds per 200 calls, using 10= input, 20 hidden and 3 output neurons), and used to be much slower untill = I fiddled about with it a bit to make it faster. However, it is still somew= hat slow for what I need it.

My question to you is if you an see any obvious (or not so obvious) way of = making this faster. I've heard about numpy and have been reading about = it, but I really can't see how it could be implemented here.

Cheers!
--
http://mail.python.org/mailman/listinfo/python-list



Low level optimizations:<= br>
If you're in Python 2.x (and not=20 3), you should use xrange() instead of range(), or maybe even create a=20 local variable and increment it and check its value within a while (that wa= y you can=20 save a few instructions on method invocations from xrange/range).

An= yways, if=20 that's not fast enough, just port it to c/c++ (or one of the=20 alternatives to speed it up while still in python: numba, cython,=20 shedskin). Or (if you can), try to use PyPy and see if you get more speed w= ithout doing anything.

Cheers,

Fabio
--001a11339edaa8d67b04e1f7cfc7--