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


Groups > comp.lang.python > #43343

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

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'sys': 0.07; 'ascii': 0.09; 'chunk': 0.09; 'data:': 0.09; 'lines.': 0.09; 'newline': 0.09; 'subject:script': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'random': 0.14; 'cc:name:python list': 0.16; 'chunk_size': 0.16; 'numpy': 0.16; 'remainder': 0.16; 'stdout': 0.16; 'subject:random': 0.16; 'wrote:': 0.18; 'seems': 0.21; 'memory': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'fairly': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'header:In-Reply- To:1': 0.27; 'specified': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'consisting': 0.31; 'file': 0.32; 'this.': 0.32; 'skip:c 30': 0.32; 'skip:# 10': 0.33; 'something': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'length': 0.61; 'simple': 0.61; 'skip:n 10': 0.64; 'more': 0.64; 'dear': 0.65; 'to:addr:gmail.com': 0.65; 'hoping': 0.75; '100': 0.79; "'0123456789'": 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=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=aT/+pX9ffnK0ixpLEwqrrdFDS527b8k3s3fztlxe1gg=; b=IMGaWkc/NnEd2wD7fCVvIx3Tyu7c3AZB8j7iHxAGRN1WC3pz72dQlVn/sHHo1X8JXh 69gchRpBFDyhRNZwpG+LyocLbuhoWDjU9RP7f0A5KARGjPVVxOm0oRQzuT8vJgB8/beW QW51wOVjDYEwd5ocNh7I7e06ID4AG9HCbeju9FsBX/aVS7nCgZHem0LEiIaQ9tVNVS4R 55fI+ZgiYxE+F8ptIodiz4fCHOH1t8y3Q9oNCUMPJaoglPjwfRtLSclmQ58SJvk8V9xl +JrKpEAACLHOWbzqFfJA8TMtqYEojHmY+oznQPt3OxzyOzjP4TUWVyMqwDPjCjX8Gfxa 7Bvw==
X-Received by 10.52.95.108 with SMTP id dj12mr505919vdb.46.1365673645787; Thu, 11 Apr 2013 02:47:25 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <24dc619b-7abd-4be3-aa92-f858eb4ab85f@n4g2000yqj.googlegroups.com>
References <24dc619b-7abd-4be3-aa92-f858eb4ab85f@n4g2000yqj.googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Thu, 11 Apr 2013 10:47:05 +0100
Subject Re: performance of script to write very long lines of random chars
To gry <georgeryoung@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
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.451.1365673654.3114.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365673654 news.xs4all.nl 2599 [2001:888:2000:d::a6]:50524
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43343

Show key headers only | View raw


On 11 April 2013 02:21, gry <georgeryoung@gmail.com> wrote:
> Dear pythonistas,
>    I am writing a tiny utility to produce a file consisting of a
> specified number of lines of a given length of random ascii
> characters.  I am hoping to find a more time and memory efficient way,
> that is still fairly simple clear, and _pythonic_.
>
> I would like to have something that I can use at both extremes of
> data:
>
>    32M chars per line * 100 lines
> or
>    5 chars per line * 1e8 lines.

I would definitely use numpy for this. The script below seems to be
io-bound on my machine:

#!/usr/bin/env python

from sys import stdout
import numpy as np
from numpy import random

CHARS = (
    '0123456789'
    'abcdefghijklmnopqrstuvwxyz'
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    '!#$%& \'()*+,-./:;<=>?@[\\]^_`{}'
    )

ARRAY_CHARS = np.frombuffer(CHARS, np.uint8)
NCHARS = len(CHARS)

CHUNK_SIZE = 4096
NCOLS = 32000000
NROWS = 10

def chunk_sizes(total, chunk_size):
    numchunks, remainder = divmod(total, chunk_size)
    for n in range(numchunks):
        yield chunk_size
    if remainder:
        yield remainder

def chunks():
    bytes_per_line = NCOLS + 1
    total_bytes = bytes_per_line * NROWS
    newline_index = NCOLS
    newline = ord('\n')
    for size in chunk_sizes(total_bytes, CHUNK_SIZE):
        chars = ARRAY_CHARS[random.randint(0, NCHARS, size)]
        chars[newline_index::bytes_per_line] = newline
        newline_index = (newline_index - CHUNK_SIZE) % bytes_per_line
        yield chars

for chunk in chunks():
    chunk.tofile(stdout)

Back to comp.lang.python | Previous | NextPrevious 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