Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Mon, 14 Mar 2016 07:57:31 +1100 Lines: 55 Message-ID: References: <56e44258$0$1598$c3e8da3$5496439d@news.astraweb.com> <8737rvxs89.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de PCtZrSxkbu6/CWU8TFbjVwZvrSHkG7m10mFF1z6mboBA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'received:209.85.223': 0.03; 'elif': 0.04; 'skip:o 50': 0.07; 'cc:addr:python-list': 0.09; 'subject:which': 0.09; 'underlying': 0.09; 'worked,': 0.09; 'python': 0.10; '(but': 0.15; 'weird': 0.15; '*do': 0.16; '2016': 0.16; 'cleaner': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'ops': 0.16; 'ranges.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'seconds,': 0.16; 'seconds.': 0.16; 'subject:?)': 0.16; 'tweak': 0.16; 'wrote:': 0.16; 'string': 0.17; 'comparing': 0.18; 'integer': 0.18; 'programmer': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'not,': 0.22; '(by': 0.22; 'am,': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'checking': 0.27; 'least': 0.27; 'message-id:@mail.gmail.com': 0.27; '14,': 0.27; 'looks': 0.29; 'code': 0.30; 'waste': 0.30; 'seconds': 0.31; 'good.': 0.32; 'class': 0.33; 'though.': 0.33; 'values.': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'replace': 0.35; 'something': 0.35; 'but': 0.36; 'too': 0.36; 'received:209.85': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'expect': 0.37; 'say': 0.37; 'received:209': 0.38; 'someone': 0.38; 'why': 0.39; 'still': 0.40; 'your': 0.60; 'subject:The': 0.61; 'valuable': 0.61; 'skip:n 10': 0.62; 'mar': 0.65; 'upper': 0.76; '3.6': 0.84; 'benchmark': 0.84; 'chrisa': 0.84; 'absolutely': 0.88; 'to:none': 0.91 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:cc; bh=ljPDKrGaDP7ALeEOFZBVjmj8jW8jkaKY1ujsQmksOZc=; b=JvOr/VYDapGvojSY+ye49v8RlR72i8m3XdyU4me/BeJh+BLjBvVpfYaYhRTOfI7qT1 MpTIKiAEJ0/nhWRK13MOh+GEhk00wxFn9KjxY3gzU/xIRcnCrYTB/LfOFsO5kffb9mXX tnSRgtzAt/4pFobB3DQb1npnF9XOzMoVZ/KHdRwmK6Jm7PI7/vXTXqdFbhTfleCILDu9 YVHxcqsniUYs7ErID59MJDs3e9DHOyEIHcYEyblW1HcrV5e1Y31dA82ttGAImQ1tQE+x 4PxhO6Hy04gOtLDcoU6DIE3G62reqsVvPIT8D3cIehJ4BkTjSxCxoSizziYM7tDxS+C2 PXAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=ljPDKrGaDP7ALeEOFZBVjmj8jW8jkaKY1ujsQmksOZc=; b=SVjvKW/besuCRA+DggBaGw5SwQ9ed5znG7ecqHHwQIq6D4mEhkULCZxHsx7BLpeHf7 0hTxKCwlo/z7zFtO/afNJZeMcFTnfU6TrPnLoFFslWRaN58eFhDuBLxCjI35nW0C/Aag nzQZsLUNgl0Gwlr7XiqkNc7DdyGFrDHBbLGrRYZTSkYbFG4ItA6X61uWwgsMYgNwxPsR bRez66FqfOu7ZKSiXPMjCw2w0RNR5CCvtxFKToktz3SOHKlW6kvDYN0PDqK0SfabN4Wd JIz+m21r9KJJAcmAMtDJwDRjj9uQjNLfhRwFE9sQhfiYHtUpBlLJFIarxTiRdkoJvYl0 QEHQ== X-Gm-Message-State: AD7BkJKFQ5VWiwmbL27q4uwcpZeDyRlfs4H1O0sGLvu3DrW/DoBCntOCF9bSsXyd+Jp5N8ZP4spNVrPHm/aySQ== X-Received: by 10.107.138.75 with SMTP id m72mr20488823iod.31.1457902651443; Sun, 13 Mar 2016 13:57:31 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104784 On Mon, Mar 14, 2016 at 6:39 AM, BartC wrote: > I used it in my benchmark to replace the if-else chain checking three lots > of ranges: > > switch(c) > if case(ord("A"),ord("B"),ord("C"),ord("D"),ord("E"),ord("F"), > ord("G"),ord("H"),ord("I"),ord("J"),ord("K"),ord("L"), > ord("M"),ord("N"),ord("O"),ord("P"),ord("Q"),ord("R"), > ord("S"),ord("T"),ord("U"),ord("V"),ord("W"),ord("X"), > ord("Y"),ord("Z")): > upper+=1 > elif case(ord("a"),ord("b"),ord("c"),ord("d"),ord("e"),ord("f"), > ord("g"),ord("h"),ord("i"),ord("j"),ord("k"),ord("l"), > ord("m"),ord("n"),ord("o"),ord("p"),ord("q"),ord("r"), > ord("s"),ord("t"),ord("u"),ord("v"),ord("w"),ord("x"), > ord("y"),ord("z")): > lower+=1 > elif case(ord("0"),ord("1"),ord("2"),ord("3"),ord("4"),ord("5"), > ord("6"),ord("7"),ord("8"),ord("9")): > digits+=1 > else: > other+=1 > > It worked, but took 110 seconds; 80 seconds without the ord's and comparing > strings (but I still think it's perverse that integer ops are slower than > string ops). > > But 110 or 80 seconds, the original Python was 3.6 seconds. (Probably, > someone could tweak it to work with ranges, but this is extra programmer > effort that you say is too valuable to waste on such matters.) This is not comparing ranges, though. This is comparing against individual values. To talk about comparing ranges, I would expect the code to look something like this: switch(c) if case("A", "Z"): upper += 1 elif case("a", "z"): lower += 1 elif case("0", "9"): digits += 1 else: other += 1 THIS is comparing ranges. The underlying comparisons must be inequalities, not equalities. I absolutely *do not care* about performance until the code looks good - at least reasonably good. (By the way, your switch/case pair is non-reentrant. Plus it uses a class in a weird way. Why not, if you're working like this, just have two functions and a module-global? Just as non-reentrant, much cleaner code.) ChrisA