Path: csiph.com!news.mixmin.net!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Delaney Newsgroups: comp.lang.python Subject: Re: How to read from a file to an arbitrary delimiter efficiently? Date: Mon, 29 Feb 2016 08:00:22 +1100 Lines: 36 Message-ID: References: <56cea44e$0$11128$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de axMz3fmDxKqmpgGYSULiuQHhUpWuXitv96DU7EL6NxtA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'method.': 0.05; 'chunk': 0.07; 'subject:file': 0.07; 'subject:How': 0.09; 'benjamin': 0.09; 'buffer,': 0.09; 'delimiter': 0.09; 'sake': 0.09; 'underlying': 0.09; '(small)': 0.16; '100x': 0.16; '2016': 0.16; "chris'": 0.16; 'delimiters': 0.16; 'dropping': 0.16; 'efficiently.': 0.16; 'example).': 0.16; 'expressions.': 0.16; 'iterating': 0.16; 'mmap': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stream.': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.16; '>': 0.18; 'email addr:gmail.com>': 0.18; 'input': 0.18; 'otherwise,': 0.20; 'tim': 0.24; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'regular': 0.29; "i'm": 0.30; 'probably': 0.31; "d'aprano": 0.33; 'lets': 0.33; 'steven': 0.33; 'though.': 0.33; '(for': 0.34; 'file': 0.34; 'received:google.com': 0.35; 'attempt': 0.35; 'whilst': 0.35; 'something': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'there': 0.36; 'lines': 0.36; 'faster': 0.36; 'to:addr:python- list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'files': 0.38; 'end': 0.39; 'sure': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'back': 0.62; 'leaving': 0.63; 'times': 0.63; '100': 0.79; 'delaney': 0.84; 'exercise,': 0.84; 'oscar': 0.84; 'overhead,': 0.84; 'subject:read': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to; bh=GzUDlvqONtVylVhb3xRwbsgHLWXM+MdyWwFPUF6K9q4=; b=UFIN4KdZugz3t/mRWJvruXTgOU0ueRIiYDdXeYOWz0TALr45I8BbzG2X6IRi5wcEc3 jeak0Ra5FDCeYKg6/TxoEaDOR6bWWyIRDMteD71YdmCs6xj6k1TnHaTnZ1oclwWYjb5p jzPRWrCrw2rPuLcJCn147gRoZNMGR6py7HYWLCBA1UbBLU3ABwHroExiBf5DL2pihkhp lyv1dr8gesSZeg/5nY3CN4e+HDDa4g3952ZRDzdxKlWAbP+29GUs/LzOmTTEXQxflaE8 r0EhEs38ebeKE50EUQ3ovoRZRbWjOIclS4MnEbgDBteLzAqjBLIE/SluyOB2tMfEfm7I 9ZyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to; bh=GzUDlvqONtVylVhb3xRwbsgHLWXM+MdyWwFPUF6K9q4=; b=iH/7pZBNSf4YAf2O6O8S0CYIGoSD/PRYeaf7jQPOKlPVUV5xAIHkwziTmmLamw5sHI H85BWa1R/Z+h0V71I8i+DuDvSq52L0hOCD43at6+y1SHWF6EpTt1DerUk7V5/6v/9xtR X01TLxzwwQWSpdhdpY4rEBbCA8SCH8Pk8CjfUJC1vWRtyqUIh84UO/XBXiM2eubMzQgS l+6uxp6b/bV0VZSS3irndzHygSVy3iVELszn+NlKtX+MENDgM62kRc7SAOVXLqoQ6XTK 9f7kWfTNc59mTz1WJQJYOITQ8siQGg8sW3M/4GIAU90nCnC/mbBcuyuuo1Og7Eh9K4IT BPWg== X-Gm-Message-State: AD7BkJJs+EQAuN2JOjJKAyAwmsRe9+fPn3QcIfTNoUEvINUG5dC9S2MWCSttmic7v6K0pd+lQcwuGT0OaUrUTg== X-Received: by 10.194.59.233 with SMTP id c9mr10887058wjr.88.1456693222200; Sun, 28 Feb 2016 13:00:22 -0800 (PST) In-Reply-To: X-Content-Filtered-By: Mailman/MimeDel 2.1.21rc2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103662 On 29 February 2016 at 07:28, Oscar Benjamin wrote: > On 25 February 2016 at 06:50, Steven D'Aprano > wrote: > > > > I have a need to read to an arbitrary delimiter, which might be any of a > > (small) set of characters. For the sake of the exercise, lets say it is > > either ! or ? (for example). > > > > I want to read from files reasonably efficiently. I don't mind if there > is a > > little overhead, but my first attempt is 100 times slower than the > built-in > > "read to the end of the line" method. > > You can get something much faster using mmap and searching for a > single delimiter: > > My timing makes that ~7x slower than iterating over the lines of the > file but still around 100x faster than reading individual characters. > I'm not sure how to generalise it to looking for multiple delimiters > without dropping back to reading individual characters though. > You can use an mmapped file as the input for regular expressions. May or may not be particularly efficient. Otherwise, if reading from a file I think read a chunk, and seek() back to the delimiter is probably going to be most efficient whilst leaving the file position just after the delimiter. If reading from a stream, I think Chris' read a chunk and maintain an internal buffer, and don't give access to the underlying stream. Tim Delaney