From: Bryn K. <xo...@xo...> - 2003-03-07 21:55:51
|
Daniel Rall wrote: >"Moyer, Todd" <TMoyer@TALK.COM> writes: > > > >>I came across the following dramatic performance improvement in my code and >>thought I'd pass it along. I know Jython isn't choosen for it's blinding >>speed, but in this case my response time went from painful to trivial. I >>did a quick search of the archives and the web in general and didn't see any >>discussion of this. Apologies if it's widely known. >> >>I was putting together a string representation of a Table object I >>developed. One of my most common uses for this is taking the output of an >>SQL select, which can get quite large. I had been doing a lot of string >>concatination (string = string + "foo"). Needless to say, this got really >>slow when the tables were big: 5,000 rows was taking almost a minute. I >>reimplemented using the Java StringBuffer's "append" method. Execution time >>dropped to about 1 second. >> >>Using the Java object in my Jython code was easy; I can post it if anyone >>wants to see it. However, is there pure Jython way to do this that I'm >>overlooking? If not, is this something worth adding to the language? >> >> > >When not in a loop or conditional, most Java compilers do this for you >now a days. Not knowing anything about the internals of Jython, I >wonder if a Python str/StringType might be better represented >internally using a StringBuffer in Jython. > Concatenating strings is a slow way of doing things in most languages, it's almost always better to use a StringBuffer or a list or something like that to collect the pieces, and make them into a string at the end. Here's a pure {P,J}ython solution that shows a big difference without having to import Java classes: import time LOOPSIZE = 20000 def slowway(): start = time.time() foo = "" for i in xrange(LOOPSIZE): foo = foo + "foo" final = foo print "Time:", time.time() - start def fastway(): start = time.time() foo = [] for i in xrange(LOOPSIZE): foo.append("foo") final = "".join(foo) print "Time:", time.time() - start #First do it the slow way (string concatentation) slowway() #Then with the fast way (Joining a list of strings together) fastway() Running this script under Jython 2.1 prints: Time: 7.0 Time: 0.09100008010864258 Regards, Bryn |