Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43308

Re: performance of script to write very long lines of random chars

Path csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'linux,': 0.07; 'character,': 0.09; 'rows': 0.09; 'subject:script': 0.09; 'windows,': 0.09; 'python': 0.11; 'random': 0.14; '64,': 0.16; 'alphabet': 0.16; 'base64': 0.16; 'fine.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'set,': 0.16; "skip:' 60": 0.16; 'subject:random': 0.16; 'wrote:': 0.18; 'module': 0.19; 'thu,': 0.19; 'import': 0.22; 'instance,': 0.24; 'simpler': 0.24; 'test.': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'character': 0.29; 'raise': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'program,': 0.31; 'lines': 0.31; 'bunch': 0.31; 'linux': 0.33; 'call.': 0.33; 'received:209.85': 0.35; 'received:209.85.220': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'acceptable': 0.36; 'should': 0.36; 'received:209': 0.37; 'depends': 0.38; 'to:addr:python-list': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'easy': 0.60; 'guarantee': 0.63; 'skip:n 10': 0.64; 'different': 0.65; 'note:': 0.66; 'fact,': 0.69; 'characters,': 0.84; '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=FRNPzvlNNTV2rjJs3iBBDdU2g3zx6RnTP1Pr+qzBvAw=; b=H4YQrnaGiwMCFSXvN4LI+RfoQhvPOpSFrNB39OB3/j0/leueEMZWGoJSHFSv+3mXtN ZkSq4RMT+TGdFjE0UyXLjfNXqbjxGZ8AKr9zQxmq3BIOFdgLxJQODaB7yRwLgHF17duc v/p2FmLEihcs6V6McDZ+CGsvkrCBV62bUftIeIZ6VbDNwezSF4/gfZvyc/g3/1YIx5El Fv8XB3N0kh4VhkoxBkvZgdnJ4Y41hjSniIy9nGEcNdsupVNSL1lSX5kWc2ut/HB5XsOb p2OBCUkyiDlSlFTMOooR2Bcmt6ScSlBNEw2VG00ID/SSZv1hpt1Ze79LR3ntGQcPpJ5M FQSQ==
MIME-Version 1.0
X-Received by 10.66.25.147 with SMTP id c19mr6548318pag.207.1365644731233; Wed, 10 Apr 2013 18:45:31 -0700 (PDT)
In-Reply-To <24dc619b-7abd-4be3-aa92-f858eb4ab85f@n4g2000yqj.googlegroups.com>
References <24dc619b-7abd-4be3-aa92-f858eb4ab85f@n4g2000yqj.googlegroups.com>
Date Thu, 11 Apr 2013 11:45:31 +1000
Subject Re: performance of script to write very long lines of random chars
From Chris Angelico <rosuav@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.434.1365644739.3114.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365644739 news.xs4all.nl 2609 [2001:888:2000:d::a6]:55851
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43308

Show key headers only | View raw


On Thu, Apr 11, 2013 at 11:21 AM, gry <georgeryoung@gmail.com> wrote:
> avail_chrs =
> '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&
> \'()*+,-./:;<=>?@[\\]^_`{}'

Is this exact set of characters a requirement? For instance, would it
be acceptable to instead use this set of characters?

avail_chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

Your alphabet has 92 characters, this one only 64... the advantage is
that it's really easy to work with a 64-character set; in fact, for
this specific set, it's the standard called Base 64, and Python
already has a module for working with it. All you need is a random
stream of eight-bit characters, which can be provided by os.urandom().

So here's a much simpler version of your program, following the
cut-down character set I offer:

import os
import base64
nchars = 32000000
rows = 10
# Note: If nchars is one higher than a multiple of 4 (eg 5, 9, 101),
# the lines will be one character short (4, 8, 100).
nchars = nchars * 3 // 4
for l in range(rows):
    print(base64.b64encode(os.urandom(nchars)).strip(b'='))


If you can guarantee that your nchars will always be a multiple of 4,
you can drop the .strip() call.

This is going to be *immensely* faster than calling random.choice()
for every character, but it depends on a working os.urandom (it'll
raise NotImplementedError if there's no suitable source). I know it's
available on OS/2, Windows, and Linux, but don't have others handy to
test. If by "a bunch of different computers" you mean exclusively
Linux computers, this should be fine.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

performance of script to write very long lines of random chars gry <georgeryoung@gmail.com> - 2013-04-10 18:21 -0700
  Re: performance of script to write very long lines of random chars Chris Angelico <rosuav@gmail.com> - 2013-04-11 11:45 +1000
    Re: performance of script to write very long lines of random chars Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-11 05:33 +0000
      Re: performance of script to write very long lines of random chars Chris Angelico <rosuav@gmail.com> - 2013-04-11 15:53 +1000
  Re: performance of script to write very long lines of random chars Michael Torrie <torriem@gmail.com> - 2013-04-10 19:52 -0600
    Re: performance of script to write very long lines of random chars gry <georgeryoung@gmail.com> - 2013-04-10 19:40 -0700
      Re: performance of script to write very long lines of random chars Chris Angelico <rosuav@gmail.com> - 2013-04-11 13:14 +1000
  Re: performance of script to write very long lines of random chars MRAB <python@mrabarnett.plus.com> - 2013-04-11 04:09 +0100
  Re: performance of script to write very long lines of random chars Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-11 07:47 +0000
    Re: performance of script to write very long lines of random chars Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-11 10:47 +0100
      Re: performance of script to write very long lines of random chars Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-11 10:50 +0000
        Re: performance of script to write very long lines of random chars Robert Kern <robert.kern@gmail.com> - 2013-04-11 16:49 +0530
        Re: performance of script to write very long lines of random chars Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-11 13:05 +0100
        Re: performance of script to write very long lines of random chars Robert Kern <robert.kern@gmail.com> - 2013-04-11 19:06 +0530
        Re: performance of script to write very long lines of random chars Chris Angelico <rosuav@gmail.com> - 2013-04-11 23:56 +1000
  Re: performance of script to write very long lines of random chars Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-11 10:47 +0100

csiph-web