Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '21,': 0.07; 'correct.': 0.07; 'subject:code': 0.07; 'oh,': 0.09; 'subject:How': 0.10; 'def': 0.12; 'translation': 0.12; 'effect,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'inputs': 0.16; 'iterator': 0.16; 'loop.': 0.16; 'loops': 0.16; 'margin,': 0.16; 'readable': 0.16; 'subject:make': 0.16; 'temp': 0.16; 'wrote:': 0.18; 'replacing': 0.19; 'input': 0.22; 'replace': 0.24; 'decide': 0.24; 'looks': 0.24; "i've": 0.25; 'this:': 0.26; 'second': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'slot': 0.31; 'probably': 0.32; 'quite': 0.32; "i'd": 0.34; "can't": 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'raising': 0.36; 'shorter': 0.36; 'next': 0.36; 'subject:?': 0.36; 'two': 0.37; 'list': 0.37; 'performance': 0.37; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'structure': 0.39; 'subject:can': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'negative': 0.60; 'entire': 0.61; 'skip:n 10': 0.64; 'confirm': 0.64; 'more': 0.64; 'finally': 0.65; 'number:': 0.66; 'jul': 0.74; 'subject:this': 0.83; 'end.': 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:date:message-id:subject:from:to :content-type; bh=GblbpJK74A6tp7cfCBgJJm9IjXKMJKZjw1DPHM3qjOI=; b=w8Zq1wbwjIpUUw4FizkdAQ1WGgmovDn2cOPXMLwhajm3z/ANUjoQVqwhMytf8VdCDD tLf5yLmJY4gkaslPLCyzTnsuH/C4rHBCdSeq90mNCXzBXcyigER51Wl4slDNxzBXi9gY hpVMlQ7YAZ3BnbQ0GGaHpEwvxap5k5hxikmM6JMwTiHU8i2t2bIf58sXl/3Da75XIqrb dH15vVfbZrd6yx+E2RRhwZzRTbp+hcRKneEUvJ6gea2e4+kFvhO9Ps6t/xGLRZ/UreKi fCoJ3At0dpoDj8rXKx22BntQUYQsnlT57AUOaalV0goQEuVaNzKVZ25v8TJJKK7m7NGp RPgg== MIME-Version: 1.0 X-Received: by 10.220.169.146 with SMTP id z18mr7589087vcy.80.1374360908076; Sat, 20 Jul 2013 15:55:08 -0700 (PDT) In-Reply-To: <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> References: <6bf4d298-b425-4357-9c1a-192e6e6cd9f0@googlegroups.com> Date: Sun, 21 Jul 2013 08:55:07 +1000 Subject: Re: How can I make this piece of code even faster? From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1374360916 news.xs4all.nl 15914 [2001:888:2000:d::a6]:58273 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50983 On Sun, Jul 21, 2013 at 6:22 AM, wrote: > temp = 0 > for y in range(input_num): > count += 1 > temp += inputs[y] * h_weight[count] > hidden[x] = 1/(1+e**(-temp)) It's a micro-optimization that'll probably have negligible effect, but it can't hurt: Instead of adding to temp and raising e to -temp, carry the value of temp as a negative number: temp -= inputs[y] * h_weight[count] hidden[x] = 1/(1+e**temp) Ditto in the second loop. Not sure which way performance would go, but would it be more readable to take an iterator for h_weight and o_weight? Something like this: # Slot this into your existing structure inputs = self.input h_weight = iter(self.h_weight) o_weight = iter(self.o_weight) e = math.e for x in range(hidden_num): temp = 0 for y in inputs: temp += y * next(h_weight) hidden[x] = 1/(1+e**(-temp)) for x in range(output_num): temp = 0 for y in hidden: temp += y * next(o_weight) output[x] = 1/(1+e**(-temp)) # End. If that looks better, the next change I'd look to make is replacing the 'for y' loops with sum() calls on generators: temp = sum(y * next(o_weight) for y in hidden) And finally replace the entire 'for x' loops with list comps... which makes for two sizeable one-liners, which I like and many people detest: def tick(self): inputs = self.inputs h_weight = iter(self.h_weight) o_weight = iter(self.o_weight) e = math.e hidden = [1/(1+e**sum(-y * next(h_weight) for y in inputs)) for _ in range(hidden_num)] self.output = [1/(1+e**sum(-y * next(o_weight) for y in hidden)) for _ in range(output_num)] Up to you to decide whether you find that version more readable, or at least sufficiently readable, and then to test performance :) But it's shorter by quite a margin, which I personally like. Oh, and I'm relying on you to make sure I've made the translation correctly, which I can't confirm without a pile of input data to test it on. All I can say is that it's syntactically correct. ChrisA