Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: BartC Newsgroups: comp.lang.python Subject: Re: The Cost of Dynamism (was Re: Pyhon 2.x or 3.x, which is faster?) Date: Fri, 25 Mar 2016 22:08:19 +0000 Organization: A noiseless patient Spider Lines: 52 Message-ID: References: <56f02196$0$1588$c3e8da3$5496439d@news.astraweb.com> <56f42d9f$0$1618$c3e8da3$5496439d@news.astraweb.com> <56f565e4$0$1614$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 25 Mar 2016 22:05:16 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="cf45b3961a050227b1103bebc3cbc15a"; logging-data="661"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/UwR/kjCGwfzONKyBZ3biM" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 In-Reply-To: <56f565e4$0$1614$c3e8da3$5496439d@news.astraweb.com> Cancel-Lock: sha1:kFSuBCc0sj2k6zFTdGNdWcsnOA4= Xref: csiph.com comp.lang.python:105711 On 25/03/2016 16:22, Steven D'Aprano wrote: > On Fri, 25 Mar 2016 10:06 pm, BartC wrote: (OK, I'll risk the wrath of Mark Lawrence et al by daring to post my opinions.) >> But it is an optimisation that /could/ be done by the byte-code compiler > I would also expect that Victor Stinner's FAT Python project will > (eventually) look at optimizing such for-loops too. If you see something > like: > > for i in range(N): > do_stuff() > > > and three conditions hold: > > (1) i is unused in the loop body; > > (2) range is still bound to the expected built-in function, and no > other "range" in a nearer scope (say, a local variable called "range", or a > global) exists to shadow it; and The volatility of 'range' I'd completely forgotten about. Python really doesn't make this stuff easy, does it? Checking this at runtime, per loop (I don't think it's necessary per iteration) is a possibility, but now an extra overhead is introduced. It might worth it if the body of the loop is also optimised, but this is getting into the territory covered by tracing JITs. My kind of approach would try to keep it simple, and that would be helped tremendously by special language constructs (something like Ruby's 100.times) where you can use a streamlined loop, and possible unrolling, straight off. (Personally, if I was creating a byte-code intepreter for Python as-is, I would have a -dynamic:on or -dynamic:off switch.) > Here is one of the oldest: > > http://www.ibm.com/developerworks/library/l-psyco/index.html Yes, I tried that long ago. It's very fast on certain benchmarks. (Both psyco and pypy can run my lexer at around 170Klps. And this is code using string compares and long if-elif chains, that would be crazy in any other language. From that point of view, it's very impressive (I can only get half that speed using the same methods). -- Bartc