Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'say,': 0.05; 'cache': 0.07; 'debugging': 0.07; 'mouse': 0.07; 'reason,': 0.07; 'url:products': 0.07; '*is*': 0.09; 'badly': 0.09; 'len(x)': 0.09; 'runtime': 0.09; 'subtle': 0.09; 'yeah,': 0.09; 'cc:addr :python-list': 0.11; 'bug': 0.12; 'windows': 0.15; '(about': 0.16; '*and': 0.16; '*any*': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'here"': 0.16; 'idle.': 0.16; 'refactoring': 0.16; 'removed,': 0.16; 'renaming': 0.16; 'somewhere.': 0.16; 'sorts': 0.16; 'subtle.': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'user.': 0.19; 'code,': 0.22; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'adds': 0.24; 'fairly': 0.24; '(or': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'references': 0.26; 'certain': 0.27; 'gets': 0.27; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'appear': 0.29; '[1]': 0.29; 'am,': 0.29; 'quickly': 0.29; 'moved': 0.30; 'nature': 0.30; 'especially': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'too.': 0.31; '(maybe': 0.31; 'crash': 0.31; 'stands': 0.31; 'time;': 0.31; 'file': 0.32; 'stuff': 0.32; 'bugs': 0.33; 'fri,': 0.33; 'actual': 0.34; 'could': 0.34; 'display': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'animation': 0.36; 'crazy': 0.36; 'disk': 0.36; 'idle': 0.36; 'object,': 0.36; 'sat': 0.36; 'sequence': 0.36; 'done': 0.36; 'doing': 0.36; 'next': 0.36; 'seconds': 0.37; 'being': 0.38; 'thank': 0.38; 'mine': 0.38; 'work?': 0.38; 'fact': 0.38; 'that,': 0.38; 'expect': 0.39; 'though,': 0.39; 'enough': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'even': 0.60; 'easy': 0.60; 'cost.': 0.60; 'affect': 0.61; 'gone': 0.61; 'completed': 0.61; 'simple': 0.61; "you'll": 0.62; 'complete': 0.62; 'show': 0.63; 'such': 0.63; 'happen': 0.63; 'more': 0.64; 'land': 0.65; 'production': 0.68; 'url:png': 0.68; 'line,': 0.68; 'miss': 0.74; 'influence': 0.74; 'present.': 0.74; 'hoping': 0.75; 'honest': 0.78; 'introduce': 0.78; 'as:': 0.81; 'url:images': 0.83; 'plants': 0.84; 'apparent': 0.91; 'to:none': 0.92; 'race': 0.95 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 :content-type; bh=+3m5lFvq5rSxu5BhkWya+E3oheBinGm89QATympRFsA=; b=e8LYDTzMu3koOM8PckP76GlDo94Mc2SA3mtEdu6jaWzfz8YwwIZAKS7OuvSs1KnRVF IvD3JUzJPpo0Arv3KzNmwqiivwYPgHBtHqAwlW+GRcwt1ki2XlzBVkVDZQNOJ2GFeX55 ++XQcR8xq4jO8dRVFZls45yWjg3IAu5JN13rPu1aFm1AZvHQuHzKCcAtH7WvTBeXEO/v iZVoySPHO0swNaNCPZ7HSWek2E4rRaF/YGZcs1Gq6u6WZDAWRoDxL/VY5Kz7+coSNRg8 lAZW4yi2VZWMRR2+q1KUeALs6ld/KPBjeOjqK4xfYmP/obCxfIUuP5jtTI9LRqpmg2Jp fqfQ== MIME-Version: 1.0 X-Received: by 10.220.94.135 with SMTP id z7mr115758vcm.46.1403834814830; Thu, 26 Jun 2014 19:06:54 -0700 (PDT) In-Reply-To: References: Date: Fri, 27 Jun 2014 12:06:54 +1000 Subject: Re: print statements and profiling a function slowed performance From: Chris Angelico Cc: "python-list@python.org" 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1403834817 news.xs4all.nl 2838 [2001:888:2000:d::a6]:53372 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:73643 On Fri, Jun 27, 2014 at 6:36 AM, CM wrote: >> Yes, it stands to reason that profiling code >> is going to introduce a runtime cost. How else >> would we expect profiling to work? > > I think I was hoping for magic. :D Thank you for being honest :) The fact is, though, that time-of-day and console output take a lot more time than many people seem to realize; this is especially true if you print something repeatedly on the same line, such as: num = len(x) for i,foo in enumerate(x): print(i/num,end="\r") # perform processing If x is, say, range(1000000), a simple "for foo in x: pass" will complete fairly quickly (maybe 100ms on my computer), while the progress-indicated loop will take much longer (about 30 seconds when I tried it). Obviously you'll be doing more work than just "pass", but it's easy to completely dwarf the actual processing with the display to the user. (And yes, this can happen in production code, too. We had an old Windows 3 program that, for some reason, completed its processing in less time if someone moved the mouse around than if it sat idle. Its stupid animation - not even a progress indication, just "hi, I'm still working here" - interacted badly with idle sensitivity.) >> What I do find Heisenbergian are bugs that show >> up when debugging and profiling stuff are removed, >> but completely gone when present. IE profiling and >> debugging slow it down enough that often subtle race >> conditions are masked. > > Would never have occurred to me. That *is* odd! Race conditions are by their nature subtle. I've seen all sorts of crazy things change their behaviour... refactoring a function can appear to introduce or eliminate a bug (because the call/return sequence adds a small delay), and occasionally, even a completely benign change can influence something - renaming a file on the disk can cause a cache miss and make the program work perfectly (or fail to work) the one next time it's run. Yeah, that can be fun. (Though not as much fun as debugging a refcount error, where a program will crash if certain things are done *and then certain others*. The actually-faulty code just plants a land mine [1], and until you tread on it, nothing goes wrong. Depending on how many other references there are to that object, the freeing could happen at any time; and even after it's freed, there might be no apparent problem, until the memory gets reused somewhere. Now THAT is fun to debug. Pretty much *any* change to the code can affect whether or not it crashes.) ChrisA [1] Like this guy. http://media.wizards.com/images/magic/tcg/products/m15/sf0JdVsk2/EN_42um78zriv.png