Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89554
| Newsgroups | comp.lang.python |
|---|---|
| Date | 2015-04-29 08:16 -0700 |
| References | <878udbxrpg.fsf@Equus.decebal.nl> <5540d95c$0$12982$c3e8da3$5496439d@news.astraweb.com> |
| Message-ID | <db3b6548-9f4c-472d-ab47-cf25653eefe6@googlegroups.com> (permalink) |
| Subject | Re: Using + with strings considered bad |
| From | wxjmfauth@gmail.com |
Le mercredi 29 avril 2015 15:15:19 UTC+2, Steven D'Aprano a écrit :
> On Wed, 29 Apr 2015 06:29 pm, Cecil Westerhof wrote:
>
> > Because I try to keep my lines (well) below 80 characters, I use the
> > following:
> > print('Calculating fibonacci and fibonacci_memoize once for ' +
> > str(large_fibonacci) + ' to determine speed increase')
>
> That's perfectly fine, but these two alternatives may be better:
>
> print('Calculating fibonacci and fibonacci_memoize once for'
> ' %s to determine speed increase' % large_fibonacci)
>
> print('Calculating fibonacci and fibonacci_memoize once for'
> ' {} to determine speed increase'.format(large_fibonacci))
>
>
>
> > But I was told that using + with strings was bad practice. Is this
> > true? If so, what is the better way to do this?
>
> *Repeated* string concatenation is bad practice. Concatenating one or two
> strings is fine. Doing it in a loop to build up a big string is bad mojo.
>
>
> # Perfectly fine:
> message = prefix + "something or other" + suffix
>
>
> # Okay, but there are better alternatives:
> for item in things:
> message = "something " + str(item)
> print(message)
>
>
> # This is asking for trouble.
> # Use ''.join(substrings) instead.
> text = ''
> for s in substrings:
> text = text + s
>
>
> The problem with the third one is that it has to make temporary strings
> which get thrown away, and that gets very expensive if there are many
> substrings. Suppose our substrings are "a", "bb", "ccc", "dddd", "eeeee",
> then the temporary strings that are made end up being:
>
> text = "a" # copies one character (maybe?)
> text = "abb" # copies three characters
> text = "abbccc" # copies six characters
> text = "abbcccdddd" # copies ten characters
> text = "abbcccddddeeeee" # copies fifteen characters
>
> So to build a string of length 15, Python ends up copying 34 or 35
> characters. As the number of substrings increases, the amount of wasted
> copying blows out: repeated string concatenation behaves quadratically,
> which is very slow.
>
> The tricky part is that Python starting from version 2.3 introduced an
> optimization that *may* avoid all those extra copying under *some*
> circumstances. So with casual testing, you might not notice the quadratic
> behaviour, and see linear behaviour.
>
> Until you rely on it being fast, and it isn't.
>
------
>>> timeit.repeat("''.join(['a', 'a']*1000)")
[35.80853889412481, 36.228519745690505, 36.15569110606032]
>>> timeit.repeat("''.join(['a', '\u3456']*1000)")
[38.041631106320324, 38.031590190424936, 38.020335857707664]
>>>
-----------
>>> timeit.repeat("''.join(['a', 'a']*1000)")
[38.94708620458107, 38.91509429875259, 38.914195952835485]
>>> timeit.repeat("''.join(['a', '\u3456']*1000)")
[58.284382998616366, 58.23354945884603, 58.27770782766095]
>>>
with a memory gain = 0
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Using + with strings considered bad Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 10:29 +0200
Re: Using + with strings considered bad Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 10:08 +0100
Re: Using + with strings considered bad Peter Otten <__peter__@web.de> - 2015-04-29 11:24 +0200
Re: Using + with strings considered bad Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 13:17 +0200
Re: Using + with strings considered bad Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 14:23 +0200
Re: Using + with strings considered bad Chris Angelico <rosuav@gmail.com> - 2015-04-29 22:55 +1000
Re: Using + with strings considered bad Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-29 23:15 +1000
Re: Using + with strings considered bad wxjmfauth@gmail.com - 2015-04-29 08:16 -0700
Re: Using + with strings considered bad Andrew Berg <aberg010@my.hennepintech.edu> - 2015-04-29 06:40 -0500
csiph-web