Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!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.051 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'sufficient': 0.05; 'promising': 0.09; 'subject:script': 0.09; 'translation': 0.12; 'random': 0.14; '(note:': 0.16; 'charset,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:random': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'instance,': 0.24; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'character': 0.29; 'message-id:@mail.gmail.com': 0.30; 'idea,': 0.31; 'guess': 0.33; 'received:209.85': 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'method': 0.36; 'received:209': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'generating': 0.39; 'to:addr:python.org': 0.39; 'skip:x 10': 0.40; 'skip:u 10': 0.60; 'full': 0.61; 'numbers': 0.61; 'profile': 0.61; 'first': 0.61; 'kind': 0.63; 'subject:long': 0.84; 'subject:very': 0.91; '2013': 0.98 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; bh=8HQ9nHLWtSd5Doo5RHVh+8na+1FfdMbXajw4dOhR1Z0=; b=0hdU2h6TgaA5PixVCXNhg2U8YAhdCw26p8SS/45GY5S9KGQQS7/V73Cqr/bTzGpTNP 6o0tyVQu3itPmGiPj7g22IbO7cXMkdnkUju4TciNwPwHDkykYlZcSD5tvmjjPp+/pPhq XY2Wkrhk3MVNoqvXqsaRA1XryrPSEaFLSkZv8izTugzsDw0StmyI1DDa5aDSqDVGaaUB pu+1RaZWOXl+UZ3Y49MntN/oAxmAzvxlW2HTwcaYZkNjONE1wm9u+XheAV3bST8dkZAZ oIh7Q4YQXeUsLQN9DxC4OJiKv3HdhzNgWQqf5vkTdEdA2ZEhjnUofHRFtGDPimpSdfM3 b9IQ== MIME-Version: 1.0 X-Received: by 10.68.243.99 with SMTP id wx3mr6239048pbc.103.1365650077151; Wed, 10 Apr 2013 20:14:37 -0700 (PDT) In-Reply-To: <15b233c5-f961-479e-aec1-fe1467bd99d3@e8g2000yqg.googlegroups.com> References: <24dc619b-7abd-4be3-aa92-f858eb4ab85f@n4g2000yqj.googlegroups.com> <15b233c5-f961-479e-aec1-fe1467bd99d3@e8g2000yqg.googlegroups.com> Date: Thu, 11 Apr 2013 13:14:37 +1000 Subject: Re: performance of script to write very long lines of random chars From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365650080 news.xs4all.nl 2615 [2001:888:2000:d::a6]:48438 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43313 On Thu, Apr 11, 2013 at 12:40 PM, gry wrote: > Appealing idea, but it's slower than the array solution: 5min 13 > secs. vs 4min 30sec for the array: > > for l in range(rows): > for i in xrange(nchars): > stdout.write(random.choice(avail_chrs)) > stdout.write('\n') > > > os.urandom does look promising -- I have to have full control over the > charset, but urandom is very fast at generating big random strings... > stay tuned... Without actually profiling it, my first guess would be that calling random.choice() for every character is an optimization target. (NOTE: Do profile it, if the urandom method isn't sufficient straight-off.) You may want to consider, for instance, generating larger random numbers and doing some kind of translation on them - which is fundamentally what the urandom/b64encode method is doing. ChrisA