Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'sys': 0.07; 'here?': 0.09; 'rows': 0.09; 'subject:script': 0.09; 'suggest': 0.14; 'random': 0.14; 'array.': 0.16; 'code?': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; "skip:' 60": 0.16; 'stdout': 0.16; 'subject:random': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'import': 0.22; 'header:User- Agent:1': 0.23; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'array': 0.29; 'characters': 0.30; 'but': 0.35; 'version': 0.36; 'possible': 0.36; 'so,': 0.37; 'performance': 0.37; 'message- id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'took': 0.61; 'first': 0.61; 'believe': 0.68; 'attempt,': 0.84; 'subject:long': 0.84; 'subject:very': 0.91 X-Virus-Scanned: amavisd-new at torriefamily.org Date: Wed, 10 Apr 2013 19:52:22 -0600 From: Michael Torrie User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0.12) Gecko/20130105 Thunderbird/10.0.12 MIME-Version: 1.0 To: python-list@python.org Subject: Re: performance of script to write very long lines of random chars References: <24dc619b-7abd-4be3-aa92-f858eb4ab85f@n4g2000yqj.googlegroups.com> In-Reply-To: <24dc619b-7abd-4be3-aa92-f858eb4ab85f@n4g2000yqj.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365645159 news.xs4all.nl 2564 [2001:888:2000:d::a6]:33151 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43309 On 04/10/2013 07:21 PM, gry wrote: > from sys import stdout > from array import array > import random > nchars = 32000000 > rows = 10 > avail_chrs = > '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%& > \'()*+,-./:;<=>?@[\\]^_`{}' > a = array('c', 'X' * nchars) > > for l in range(rows): > for i in xrange(nchars): > a[i] = random.choice(avail_chrs) > a.tofile(stdout) > stdout.write('\n') > > This version using array took 4 min, 29 sec, using 34MB resident/110 > virtual. So, much smaller than the first attempt, but a bit slower. > Can someone suggest a better code? And help me understand the > performance issues here? Why are you using an array? Why not just rely on the OS to buffer the output. Just write your characters straight to stdout instead of placing them in an array. At that point I believe this program will be as fast as is possible in Python.