Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.x-privat.org!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'else:': 0.03; 'interpreter': 0.04; 'subject:Python': 0.05; 'versions,': 0.05; 'including,': 0.07; 'suffix': 0.07; 'python': 0.09; 'coding,': 0.09; 'difference,': 0.09; 'portions': 0.09; 'programmer': 0.11; 'language': 0.14; "'int'": 0.16; '107': 0.16; '3.3,': 0.16; 'arrays,': 0.16; 'basic.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'happily': 0.16; 'py3': 0.16; 'result:': 0.16; 'run.': 0.16; 'sits': 0.16; 'subject:3.3': 0.16; 'syntax,': 0.16; 'wrote:': 0.17; 'integer': 0.17; 'pace': 0.17; 'code,': 0.18; 'windows': 0.19; 'app': 0.19; 'feb': 0.19; 'translate': 0.20; 'equivalent': 0.20; 'sort': 0.21; 'bit': 0.21; 'import': 0.21; 'fairly': 0.21; 'lets': 0.22; "world's": 0.22; 'runs': 0.22; "i'd": 0.22; 'seems': 0.23; 'project,': 0.24; 'idea': 0.24; 'machine': 0.24; 'header:In-Reply-To:1': 0.25; 'wrote': 0.26; 'values': 0.26; 'am,': 0.27; 'separate': 0.27; '2.6': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'rest': 0.28; 'received:209.85.212': 0.28; 'all.': 0.28; 'run': 0.28; 'coded': 0.29; 'comparison': 0.29; 'surprised': 0.29; 'types.': 0.29; 'yields': 0.29; 'usually': 0.30; 'basic': 0.30; 'seconds': 0.30; 'code': 0.31; 'skip:8 10': 0.32; 'running': 0.32; 'certain': 0.33; 'purposes,': 0.33; 'problem': 0.33; 'to:addr :python-list': 0.33; 'version': 0.34; 'received:google.com': 0.34; 'project': 0.34; 'needed': 0.35; 'adds': 0.35; 'compared': 0.35; 'faster': 0.35; 'mine': 0.35; 'similar': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'really': 0.36; 'but': 0.36; 'useful': 0.36; 'too': 0.36; 'skip:p 20': 0.36; 'does': 0.37; 'level': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'short': 0.39; 'little': 0.39; 'where': 0.40; 'skip:" 10': 0.40; 'your': 0.60; 'john': 0.60; 'range': 0.60; 'high': 0.61; 'first': 0.61; "you'll": 0.62; 'leaving': 0.62; 'different': 0.63; 'information': 0.63; 'more': 0.63; 'within': 0.64; 'subject:. ': 0.66; 'soon': 0.70; 'bulk': 0.78; '*really*': 0.84; '1980s': 0.84; '2013': 0.84; 'but!': 0.84; "everything's": 0.84; 'exercise,': 0.84; "it'd": 0.84; 'sec.': 0.84; 'subject:Basic': 0.84; 'take,': 0.84; 'yours.': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type:content-transfer-encoding; bh=ZC1Ca1LvzZSxu6C5W++6G9tXaWvrjunVLY+VZ03ULZ8=; b=k9nHm4N6JJjVhQAScKW8nW76M0Fabn7BzyGxDGtKpdKhTh+mEg8mP+APTV6f0zAZBk E8RMvJ9kGNxXqFPgTJ/JP5rKOUgxAJYYDoK4c8m2K5x393TG3C/ZyNqWy/BJXR5rYTMR R2J0dpblrs3Kf9Y4lthfT5lqb0UpQScRCf09kkctFH31JV7OrhmYkVkPOQq6Q6gLFSYm gtzdedoKFKyxPEoY8V8fQ4QHQuUxidMRZyjNxAU7A5VJZtKQscBmCgt9nDFF2V8HYSe8 sI4p3o1g7um8CEGRDyPLFpEfeX5AD71RJXLWhWcmYjWgmALdCDB23qYnsz/RiFt3V/kv HE8w== MIME-Version: 1.0 X-Received: by 10.52.88.197 with SMTP id bi5mr15370632vdb.58.1361224521501; Mon, 18 Feb 2013 13:55:21 -0800 (PST) In-Reply-To: References: Date: Tue, 19 Feb 2013 08:55:21 +1100 Subject: Re: Python 3.3 vs. MSDOS Basic From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 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: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361224523 news.xs4all.nl 6919 [2001:888:2000:d::a6]:49930 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39131 On Tue, Feb 19, 2013 at 6:13 AM, John Immarino wrote: > I coded a Python solution for Problem #14 on the Project Euler website. I= was very surprised to find that it took 107 sec. to run even though it's a= pretty simple program. I also coded an equivalent solution for the proble= m in the old MSDOS basic. (That's the 16 bit app of 1980s vintage.) It ran= in 56 sec. Is there a flaw in my coding, or is Python really this slow in = this particular application. MSDOS Basic usually runs at a snails pace comp= ared to Python. BASIC does a lot less. If you wrote an 8086 assembly language interpreter in Python, it'd run fairly slowly too :) Python isn't really the world's best language for number crunching inside a machine word; though if this were a major project, I would recommend looking into Cython, as it lets you translate a few critical portions of your code to C while leaving the rest in Python. In order to get some useful stats, I added a little timing code to your original; on my Windows XP laptop, running Python 3.3, your version took 212.64 seconds to get to a result (namely, 837799 with a count of 524). Here's how I'd code it: import time start=3Dtime.time() max=3D0 for m in range(1,1000001): n=3Dm count=3D0 while n>1: if n%2: n=3D3*n+1 else: n//=3D2 count+=3D1 if count>max: max,num=3Dcount,m if not m&16383: print("->",m,count) print(num,max) print(time.time()-start) (You'll see the same timing information that I added to yours. It adds immeasurably to the run-time, and gives some early idea of how it's going.) Running under Python 2.6, both your version and mine take about 90 seconds to run. But under Python 3.3, where (among other things) range() yields values lazily, my version is significantly faster than yours. BUT! Both versions, under 3.3, are significantly *slower* than under 2.6. My first thought is that it's because Py2 has different types for 'int' and 'long', and Py3 doesn't (effectively, everything's a long), so I added an L suffix to every number and ran each of them under 2.6 again. Seems that was the bulk of the difference, though not all. Pythonistas, does this count as a regression, or is Python sufficiently "not a number crunching language" that we don't care? (range =3D my code, as above; while =3D original version with a C-style loop counter) range py3: 171.07846403121948 while py3: 212.64104509353638 range py2: 87.859000206 while py2: 86.4059998989 range py2 longs: 190.530999899 while py2 longs: 176.125999928 For comparison purposes, I also coded up the equivalent in Pike. Pike's a very similar language to Python, but with a C-like syntax, and certain optimizations - including, significantly to this exercise, an integer type that sits within a machine word if it can (though it'll happily go arbitrary precision when it's needed to). It pretends to the programmer that it's a Py3-style "everything's an int", but underneath, functions more like Py2 with separate short and long types. The result: 22.649 seconds to reach the same conclusion. How long did your BASIC version take, and how long did the Python version on the same hardware? This sort of pure number crunching isn't really where a modern high level language shines. You'll come to *really* appreciate Python as soon as you start working with huge arrays, dictionaries, etc. This is a job for C, really. ChrisA