tpc at csua.berkeley.edu
tpc at csua.berkeley.edu
Wed Jun 11 10:06:11 EST 2003
curiosity impels me, if list append is most efficient in concatenating to the end of a string, how would I use it where I previously had string concatenation ? I can only print out the values surrounded by [[''],[''], etc]: <code> import sys dict1 = { '0' : 'zero', '1' : 'one', '2' : 'two', '3' : 'three', '4' : 'four', '5' : 'five', '6' : 'six', '7' : 'seven', '8' : 'eight', '9' : 'nine', ' ' : '\n', } def convert(numbers): w = [] for argument in numbers: w.append([dict1[digit] for digit in argument]) # w += ''.join([dict1[digit] for digit in argument]) return w </code> On Tue, 10 Jun 2003, Jonathan Gardner wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > On Tuesday 10 June 2003 07:49, Scott Russell wrote: > > On Tue, 2003-06-10 at 09:35, Jonathan Gardner wrote: > > > B) > > > s = [] > > > s.append('something') > > > s.appent('something more') > > > ... > > > print "".join(s) > > > > > > B is far more efficient than A. This really shows up when you are > > > concatenating hundreds or more elements. Anything below that, it really > > > doesn't make too much of a difference. > > > > > > > Momma always says, "Make sure you can read it first, only optimize it > > after you profile." :) > > > > You're right. This particular algorithm has been tested and profiled. I gave > you the numbers I have found when I have done profiling. That is why I use > this almost everywhere I have to concatenate multiple strings together. While > it is true you shouldn't spend too much time optimizing on your first > iteration of writing your code, it is silly to use methods you know are > inefficient when comparable methods that are efficient exist and are > well-tested. It just makes more work for the second and beyond iterations. > > - -- > Jonathan Gardner <jgardner at jonathangardner.net> > (was jgardn at alumni.washington.edu) > Live Free, Use Linux! > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.2.1 (GNU/Linux) > > iD8DBQE+5jEKWgwF3QvpWNwRAtoKAKC7By+oNZoYeO6o8XYm3i5phzjXhACfUC+o > p82lLQ5P+TDytSP2zHDkZHE= > =gHW1 > -----END PGP SIGNATURE----- > > _______________________________________________ > Mod_python mailing list > Mod_python at modpython.org > http://mailman.modpython.org/mailman/listinfo/mod_python >
|