Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.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.013 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'python3': 0.07; '"if': 0.09; 'falls': 0.09; 'sake': 0.09; 'slow.': 0.09; 'subject:test': 0.09; '(3,': 0.16; '10000000': 0.16; '24,': 0.16; 'lambda': 0.16; 'lambda:': 0.16; 'certainly': 0.24; 'simpler': 0.24; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'easier': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'probably': 0.32; 'fri,': 0.33; 'third': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'being': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'more': 0.64; '10:': 0.84; 'or:': 0.84; 'premature': 0.84; 'victim': 0.84; 'subject::': 0.85 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 :content-type; bh=KSTLd0fdIHvOl7ZIBSwbASZiHdflAdLWGaKJp8InWR4=; b=fnwkRXS2lcFQEEWnibNIqkoWf1cwjU3mgz7N5yJq0K7Q3kAhHipQNbDMFVsa6r09OH IuaVN//ee28G9nFrIJs8fTzaEZL34JMe+xGROFnaK9GAzhozYbLyROQVvJd0RZf5tr// hI7pKQz1b/R0aKdT7jlCTFLPM8i5iLA/HH7ZTgheD7So3nE4ToD9QSqvypbL8Yc7YgQq yNNLU39bKQp/mtgNRHygN+h08vy+yHYFKkIote2nittNJONuS7t2ZUsNLJ24IjFlqTim jQtScFj2z3m0mHb1yxbw+RyCWzFrwa0Wq6iJYZm8EfAbRALvsksDo8+q5A9bs2HiCQoo g+fw== X-Received: by 10.68.65.34 with SMTP id u2mr5643240pbs.6.1414174994100; Fri, 24 Oct 2014 11:23:14 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <544a4ef7$0$12984$c3e8da3$5496439d@news.astraweb.com> References: <54476af8$0$21651$426a74cc@news.free.fr> <7839376e-fc27-4299-ae63-4ddf17ef9a8a@googlegroups.com> <7d2ea3c1-504e-4f5c-8338-501b1483df38@googlegroups.com> <878uk6x5nd.fsf@elektro.pacujo.net> <544a1016$0$13000$c3e8da3$5496439d@news.astraweb.com> <87tx2tvmea.fsf@elektro.pacujo.net> <544a4ef7$0$12984$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Fri, 24 Oct 2014 12:22:33 -0600 Subject: Re: (test) ? a:b To: Python Content-Type: text/plain; charset=UTF-8 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: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1414174996 news.xs4all.nl 2949 [2001:888:2000:d::a6]:46440 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:79600 On Fri, Oct 24, 2014 at 7:07 AM, Steven D'Aprano >> if j < 10: >> j += 1 >> else: >> j = 3 >> >> or: >> >> j = j + 1 if j < 10 else 3 >> >> or: >> >> j = (lambda: 3, lambda: j + 1)[j < 10]() > > Certainly not the third one. That's needlessly obfuscated for the sake of > premature optimization. This version is much better, and probably not only > simpler and easier to read but probably more efficient too: > > j = (3, j + 1)[j < 10] Yes, the lambda approach falls victim to function calls being slow. $ python3 -m timeit -s "j = 5" "if j < 10: j+=1 > else: j=3" 10000000 loops, best of 3: 0.0513 usec per loop $ python3 -m timeit -s "j = 5" "j = j + 1 if j < 10 else 3" 10000000 loops, best of 3: 0.0519 usec per loop $ python3 -m timeit -s "j = 5" "j = (3, j+1)[j < 10]" 10000000 loops, best of 3: 0.0883 usec per loop $ python3 -m timeit -s "j = 5" "j = (lambda: 3, lambda: j+1)[j < 10]()" 1000000 loops, best of 3: 0.312 usec per loop