Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'modified': 0.05; 'socket': 0.05; 'c++,': 0.07; 'lines.': 0.07; 'parser': 0.07; 'parsing': 0.07; 'rewrite': 0.07; 'subject:help': 0.07; 'python': 0.09; '(actually': 0.09; 'expectation': 0.09; 'splitting': 0.09; 'subject:skip:m 10': 0.09; 'subject:string': 0.09; 'contribute': 0.10; 'language': 0.14; 'sat,': 0.15; '"good': 0.16; 'anyway).': 0.16; 'measured': 0.16; 'module).': 0.16; 'pythonic': 0.16; 'splits': 0.16; 'using,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'byte': 0.17; 'bytes': 0.17; 'jan': 0.18; 'code,': 0.18; 'input': 0.18; 'module': 0.19; 'written': 0.20; 'parse': 0.22; 'machine': 0.24; 'header:In-Reply- To:1': 0.25; 'wrote': 0.26; 'am,': 0.27; 'implemented': 0.27; 'c++': 0.27; 'message-id:@mail.gmail.com': 0.27; 'went': 0.28; 'all.': 0.28; 'chris': 0.28; 'fine': 0.28; '(since': 0.29; 'ansi': 0.29; 'though.': 0.29; 'probably': 0.29; 'code': 0.31; '(and': 0.32; 'point,': 0.33; 'handle': 0.33; 'to:addr:python-list': 0.33; 'version': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'text': 0.34; 'faster': 0.35; 'machines': 0.35; 'protocol': 0.35; 'received:209.85.220': 0.35; 'subject:?': 0.35; 'similar': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; "wasn't": 0.36; "didn't": 0.36; 'client': 0.36; 'test': 0.36; 'too': 0.36; 'level': 0.37; 'two': 0.37; 'uses': 0.37; 'being': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'performance': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'most': 0.61; 'high': 0.61; 'subject:Need': 0.61; 'between': 0.63; 'different': 0.63; 'more': 0.63; 'subject:. ': 0.66; 'miss': 0.75; '2013': 0.84; 'divide': 0.84; 'running,': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=DqhpsK5J5weXD2Ggb9oynNgtYxoh/N0LFbzI3RLS7Xo=; b=cUdgFe2RrPZtRiuOrMqcoD3YCel489xZ5sKDPeJn98wtMekXxoju7wyS2t5REf46B8 EkddaNT6Kjh7Xn1xbtLHVicU+2GgLCLz5K0mmy02g7yNSEyNhvfso4Ugpp74LwjSHO3p /+NdpNgIT5u0dhuQN5duyEqwUjoNBDcGTuLBVrsF71W13HYZ8K7DGj/MQ/Hp+eYXWA8T i5e+hDm/xqfhMRjMkjwdi25LjP05H2RbvGfIeg3yoo6uOerLumMfEFoBsA6U7/8X7SIu kEiIMH8SQMsSqiIUjQkLrNyIDlsotJBhTfqBT+r/1LzJmgIGC2rGn8P4nSt5u100A+eP TuGQ== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Sat, 5 Jan 2013 13:04:56 -0700 Subject: Re: Need a specific sort of string modification. Can someone help? To: Python 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1357416824 news.xs4all.nl 6870 [2001:888:2000:d::a6]:52973 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:36205 On Sat, Jan 5, 2013 at 8:57 AM, Chris Angelico wrote: > You miss my point, though. I went for simple Pythonic code, and never > measured its performance, on the expectation that it's "good enough". > Written in C, the state machine is probably WAY faster than splitting > and then iterating. My C++ MUD client uses code similar to that to > parse TELNET and ANSI codes from a stream of bytes in a socket (and > one of its "states" is that there's no more data available, so wait on > the socket); the rewrite in a high level language divides the string > on "\xFF" for TELNET and "\x1B" for ANSI, working them separately, and > then afterward splits on "\n" to divide into lines. The code's much > less convoluted, it's easier to test different parts (because I can > simply call the ANSI parser with a block of text), and on a modern > computer, you can't see the performance difference (since you spend > most of your time waiting for socket data anyway). Anecdotally and somewhat off-topic, when I wrote my own MUD client in Python, I implemented both TELNET and ANSI parsing in Python using a state machine processing one byte at a time (actually two state machines - one at the protocol layer and one at the client layer; the telnet module is a modified version of the twisted.conch.telnet module). I was worried that the processing would be too slow in Python. When I got it running, it turned out that there was a noticeable lag between input being received and displayed. But when I profiled the issue it actually turned out that the rich text control I was using, which was written in C++, wasn't able to handle a large buffer gracefully. The parsing code in Python did just fine and didn't contribute to the lag issue at all.