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


Groups > comp.lang.python > #32202

Re: better way for ' '.join(args) + '\n'?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'args': 0.04; 'intermediate': 0.05; 'append': 0.07; 'function,': 0.07; 'repeated': 0.07; 'indeed,': 0.09; 'oh,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'cleanly': 0.16; 'commandline': 0.16; 'concatenate': 0.16; 'eckhardt': 0.16; 'generator.': 0.16; 'preserving': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'tcp': 0.16; 'tempted': 0.16; 'string': 0.17; 'wrote:': 0.17; 'helper': 0.17; 'yield': 0.17; 'versions': 0.20; 'explicit': 0.22; 'implicit': 0.22; 'this:': 0.23; 'least': 0.25; 'header:User- Agent:1': 0.26; 'creating': 0.26; 'right.': 0.27; 'header:X -Complaints-To:1': 0.28; 'lines': 0.28; 'loop,': 0.29; 'overhead': 0.29; 'though.': 0.29; 'no,': 0.29; "i'm": 0.29; "skip:' 10": 0.30; 'function': 0.30; 'channel': 0.32; 'not.': 0.32; 'could': 0.32; 'message.': 0.33; 'avoiding': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'version': 0.34; 'text': 0.34; 'done': 0.34; 'sequence': 0.35; 'so,': 0.35; 'doing': 0.35; 'list.': 0.35; 'received:org': 0.36; 'but': 0.36; 'test': 0.36; 'two': 0.37; 'why': 0.37; 'rather': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'called': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'save': 0.61; 'between': 0.63; 'times': 0.63; 'unnecessary': 0.65; 'dear': 0.66; 'counts': 0.81; 'alternative.': 0.84; 'expressive': 0.84; 'original.': 0.84; 'subject:better': 0.84; 'doubling': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: better way for ' '.join(args) + '\n'?
Date Fri, 26 Oct 2012 10:58:56 +0200
Organization None
References <utrpl9-udk.ln1@satorlaser.homedns.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849ef2.dip.t-dialin.net
User-Agent KNode/4.7.3
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.2888.1351241912.27098.python-list@python.org> (permalink)
Lines 54
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1351241912 news.xs4all.nl 6959 [2001:888:2000:d::a6]:55982
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:32202

Show key headers only | View raw


Ulrich Eckhardt wrote:

> Hi!
> 
> General advise when assembling strings is to not concatenate them
> repeatedly but instead use string's join() function, because it avoids
> repeated reallocations and is at least as expressive as any alternative.
> 
> What I have now is a case where I'm assembling lines of text for driving
> a program with a commandline interface. In this scenario, I'm currently
> doing this:
> 
>    args = ['foo', 'bar', 'baz']
>    line = ' '.join(args) + '\n'
> 
> So, in other words, I'm avoiding all the unnecessary copying, just to
> make another copy to append the final newline.
> 
> The only way around this that I found involves creating an intermediate
> sequence like ['foo', ' ', 'bar', ' ', 'baz', '\n']. This can be done
> rather cleanly with a generator:
> 
>    def helper(s):
>        for i in s[:-1]:
>             yield i
>             yield ' '
>        yield s[-1]
>        yield '\n'
>    line = ''.join(tmp(args))
> 
> Efficiency-wise, this is satisfactory. 

No, it is not. In a quick timeit test it takes 5 to 10 times as long as the 
original. Remember that function calls are costly, and that with s[:-1] you 
are trading the extra string for an extra list. Also, you are doubling the 
loop implicit in str.join() with the explicit one in your oh-so-efficient 
generator.

> However, readability counts and
> that is where this version fails and that is the reason why I'm writing
> this message. So, dear fellow Pythonistas, any ideas to improve the
> original versions efficiency while preserving its expressiveness?
> 
> Oh, for all those that are tempted to tell me that this is not my
> bottleneck unless it's called in a very tight loop, you're right.
> Indeed, the overhead of the communication channel TCP between the two
> programs is by far dwarving the few microseconds I could save here. I'm
> still interested in learning new and better solutions though.

Even if it were the bottleneck the helper generator approach would still be 
unhelpful.


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


Thread

better way for ' '.join(args) + '\n'? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-26 09:49 +0200
  Re: better way for ' '.join(args) + '\n'? Peter Otten <__peter__@web.de> - 2012-10-26 10:58 +0200
  Re: better way for ' '.join(args) + '\n'? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 09:36 +0000
  Re: better way for ' '.join(args) + '\n'? Hubert Grünheidt <hgruenheidt@t-online.de> - 2012-10-26 18:21 +0200
  Re: better way for ' '.join(args) + '\n'? Tycho Andersen <tycho@tycho.ws> - 2012-10-26 16:26 -0500
  Re: better way for ' '.join(args) + '\n'? Dave Angel <d@davea.name> - 2012-10-26 17:36 -0400
  Re: better way for ' '.join(args) + '\n'? Tycho Andersen <tycho@tycho.ws> - 2012-10-26 16:42 -0500
    Re: better way for ' '.join(args) + '\n'? Ramchandra Apte <maniandram01@gmail.com> - 2012-11-03 01:26 -0700
    Re: better way for ' '.join(args) + '\n'? Ramchandra Apte <maniandram01@gmail.com> - 2012-11-03 01:26 -0700
  Re: better way for ' '.join(args) + '\n'? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-10-27 11:26 +0200
    RE: better way for ' '.join(args) + '\n'? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-29 22:28 +0000

csiph-web