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: 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 Date: Thu, 11 Apr 2013 10:47:05 +0100 Subject: Re: performance of script to write very long lines of random chars To: gry Content-Type: text/plain; charset=ISO-8859-1 Cc: Python List 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: 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 On 11 April 2013 02:21, gry 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)