Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Pyhon 2.x or 3.x, which is faster? Date: Tue, 8 Mar 2016 06:10:42 +1100 Lines: 108 Message-ID: References: <87d1r6iltx.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de u7F+qyylTMyi8+++gjROXAxHOZLFY/xeOw9qEnDqim8Q== 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; 'url:pypi': 0.03; 'broken': 0.03; 'python3': 0.05; '(so': 0.07; 'except:': 0.07; 'pypy': 0.07; 'stops': 0.07; 'cc:addr:python-list': 0.09; 'it;': 0.09; 'library?': 0.09; 'python:': 0.09; 'subject:which': 0.09; 'yeah,': 0.09; 'bug': 0.10; 'python': 0.10; 'exception': 0.13; 'def': 0.13; 'things.': 0.15; "(i'm": 0.16; '2016': 0.16; 'authors.': 0.16; 'badly.': 0.16; 'crammed': 0.16; 'decoding': 0.16; 'drag': 0.16; 'exactly?': 0.16; 'exception")': 0.16; 'file).': 0.16; 'formats,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'messy': 0.16; 'mixture': 0.16; 'old-style': 0.16; 'ported': 0.16; 'py3': 0.16; 'pythonic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reraise': 0.16; 'still,': 0.16; 'trivially': 0.16; 'try/except': 0.16; 'useless': 0.16; 'utterly': 0.16; 'wrote:': 0.16; 'looked': 0.16; "wouldn't": 0.16; 'implementing': 0.18; 'result,': 0.18; 'try:': 0.18; '>>>': 0.20; 'load': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'work,': 0.21; '(the': 0.22; 'meant': 0.22; '"",': 0.22; 'file.': 0.22; 'trying': 0.22; 'am,': 0.23; '(or': 0.23; 'feb': 0.23; 'wrote': 0.23; '(you': 0.23; "haven't": 0.24; 'tried': 0.24; '(most': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; "i've": 0.25; "doesn't": 0.26; 'linux': 0.26; 'chris': 0.26; 'figure': 0.27; 'bugs': 0.27; 'message- id:@mail.gmail.com': 0.27; 'function': 0.28; 'skip:( 20': 0.28; 'looks': 0.29; '20%': 0.29; 'complain': 0.29; 'does,': 0.29; 'faster,': 0.29; "people's": 0.29; 'code:': 0.29; 'raise': 0.29; "i'm": 0.30; 'print': 0.30; 'classes': 0.30; 'code': 0.30; 'mention': 0.30; 'task': 0.30; 'error.': 0.31; "can't": 0.32; "who's": 0.32; 'useful': 0.33; 'url:python': 0.33; 'traceback': 0.33; 'version:': 0.33; 'tue,': 0.34; 'file': 0.34; 'except': 0.34; 'worked': 0.34; 'skip:d 20': 0.34; 'gets': 0.35; 'received:google.com': 0.35; 'next': 0.35; 'could': 0.35; 'c++': 0.35; 'formats': 0.35; 'quite': 0.35; 'something': 0.35; 'supports': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; '(and': 0.36; 'smaller': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'times': 0.63; 'pleased': 0.64; 'viewing': 0.64; 'mar': 0.65; 'virtually': 0.66; 'life': 0.67; 'yourself': 0.73; 'obvious': 0.76; '"active': 0.84; '5.3.1': 0.84; 'actually,': 0.84; 'chrisa': 0.84; 'difference.': 0.84; "else's": 0.84; 'free),': 0.84; 'presumably': 0.84; 'teeth': 0.84; 'to:none': 0.91; '"one': 0.91; 'colour': 0.91; 'maybe,': 0.91; 'results,': 0.91; 'poorly': 0.93; '30%': 0.97 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=dr/38OXBYBqv3DBieJUtvdcT6GfNjwaCC46bFUXneWc=; b=QXNwro07vt/o9uAap/tS6PEFoK5kqx2fS5Nz3BKtGqZkfTGltsu0ix+Q/0nhCHsQOf 3NF+ZpLzabBQMsSuub6eHWfT23foPagFGbxd+OZ96ebWYwdFHureHwTYzQOWvW2ry6EB NATTeMN9PYMHFBp/+G6ei1N/uaewNcD5SHdAuZgPW1noVr0PuDpH0+7hqKqS7cL6ss4E UzfTZRzc46Pj1y9xfcylcgOTp9RWaa7vK/CMy7oMiIWpj4e0aNN/TvIFs6f0FB6VD59S so2237QbwEEASbu76oYxH3YGyHf/hK34qxjnkS7xiq5Eo8CkzvRic3lXnhaResgn34Wg wODQ== 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=dr/38OXBYBqv3DBieJUtvdcT6GfNjwaCC46bFUXneWc=; b=VcFJ+C7c7KWQgzph3oeAKdWyEJlkGHArp3Zst2iZcJIo1WpgUVPA30GZx/kofcR5aA SIrrpFwtVvngqobaINBZqwgR171sOlbh8RlW8TiqOBb2VfrpoPGZbujmj3H/s/gLjCgY qwZz1y6TRwWKNj/8Nu9UZlqMDg+uHXFaQVp8dAXDX2p0q2zjTWFkIUcF7uNpGcMlJf+q QjTAkQBFyBOl7+HM+2JyeuGMLA05wBbBoWbAcDZJWfeAh0v5RSe7Lk5qYhL9ZXAgbK+F PwT72Zgc17rVyosX6LdTCpMzJWdne21OZxkV79E0jRE+i0ziIGV4sJdVSG3mRhlVwfHs UJVg== X-Gm-Message-State: AD7BkJKwx+tSjumpF5WRxq2bWD8N0fqbiD1tKTnYfbPiEwTDNWD3Y3kxozom3g/apo7r/RTr1k2cvR4tfGgkFg== X-Received: by 10.50.57.11 with SMTP id e11mr13796414igq.13.1457377842702; Mon, 07 Mar 2016 11:10:42 -0800 (PST) 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:104247 On Tue, Mar 8, 2016 at 5:34 AM, BartC wrote: > On 07/03/2016 15:31, Chris Angelico wrote: >> >> On Tue, Mar 8, 2016 at 12:25 AM, BartC wrote: > > >>> (The Python version of that program is here: >>> http://pastebin.com/cHx3UhQb. It should work with any Python.) >> >> >> Actually, it won't work with any Python - not if it gets a broken >> file. Your abortjpeg() function doesn't work :) > > > What does it do when it doesn't work? I just get 'can't load file' then it > stops if there's no such file. With an extra message if there's a format > error. (I'm not sure what 'raise' does, but it doesn't complain about it. I > think I realised I didn't know what came next and left it.) Here's your code: def abortjpeg(mess): print ("Error:",mess) raise Here's my Python: $ python3 Python 3.6.0a0 (default:bbfbde6ee9d0, Feb 19 2016, 12:43:06) [GCC 5.3.1 20160121] on linux Type "help", "copyright", "credits" or "license" for more information. >>> raise Traceback (most recent call last): File "", line 1, in RuntimeError: No active exception to reraise The only try/except in your code (an except block is the only way you'll have an "active exception") is this abomination: def getfile(file): try: data=open(file,"rb").read() except: return 0 ... >> But what you have there is a messy mixture of old-style classes used >> as if they were C structs, array.array() as if it were a C array, and >> utterly uncommented code that looks like it was ported from, again, C. >> Benchmarking Py2 vs Py3 on that kind of code is like trying to figure >> out whether it's easier to drag a 747 by your teeth or your toes. >> Yeah, you might get a result, but it's a useless one. > > > I disagree. The program does its job perfectly (you will have to take it > further to verify the results, such as writing out the .ppm file and viewing > the contents). > > But Py3 is slower doing this task than Py2 by 10 or 20% (or even 30% for a > smaller file). /This is in line with other observations./ What's your meaning of "perfectly"? You're implementing things in a very C way, and then showing that two different Python interpreters have different performance. You haven't actually shown how a properly-written program would perform. >> You're not going to prove anything useful about Python - any version >> of Python - by using it badly. Try implementing JPEG decoding in a >> more Pythonic way - or, better still, use pillow and don't write that >> kind of code yourself at all. > > Before I wrote this, I looked at three other existing Python decoders, all > more Pythonic (one had been ported from C++ I think and was crammed full of > classes). > > One worked so poorly that it was dismissed. The other two were full of bugs > and didn't fully support the 3 main colour formats, but when they did work, > were 3-6 times slower than my version IIRC. > > They also only worked with Python 2 (so presumably would have been even > slower on 3!) It also meant I couldn't test PyPy on them. I tried psyco but > it made little difference. And actually the main purpose was to have > something substantial to try PyPy on. > > So I don't know who's using Python more badly: me or those other authors. Did you try the 'pillow' library? https://pypi.python.org/pypi/Pillow It supports 2.6/2.7 and 3.2+, and would be most people's "one obvious way" to do things. At very least, it should get a mention in your performance comparisons. > (I'm quite pleased with my version: smaller, faster, works on all the > Pythons, supports all 3 colour formats and no decoding bugs that I'm aware > of, and it's the first Python program I've written that does something > useful.) "all the Pythons" meaning what, exactly? And, no decoding bugs? Maybe, but I wouldn't bet my life on it. Unless your code is a pure port of someone else's (and unless that someone else could guarantee that the original C code was bug free), I wouldn't trust code that I can't completely analyze in one sitting. Virtually all code has bugs in it; the only code that doesn't is code that's so trivially simple that you can completely understand it in one "headspace". ChrisA