2014. 1. 10.

Compare running time of String concat methods in python ( only time)

I just describe about concat methods  at previous post. Some guy asked me 'just method ?'  and I answered  'I will test'.


Only run twice about tree methods.  This test show only running time.  It does not consider memory usages. Next time I will consider both of them (time and memory).

If you have any advice. comment please.
Thanks.



==Test result. ==


2014-01-10 10:39:56,700 INFO -------------------------------------
2014-01-10 10:39:56,700 DEBUG START(Normal string Plus)
2014-01-10 10:40:06,388 DEBUG END: running time==>9


2014-01-10 10:40:06,390 DEBUG START(StreamWriter)
2014-01-10 10:42:15,395 DEBUG END: running time==>129


2014-01-10 10:42:15,395 DEBUG START(List and Join)
2014-01-10 10:42:27,121 DEBUG END: running time==>11


2014-01-10 10:42:56,802 INFO -------------------------------------
2014-01-10 10:42:56,802 DEBUG START(Normal string Plus)
2014-01-10 10:43:06,098 DEBUG END: running time==>9


2014-01-10 10:43:06,098 DEBUG START(StreamWriter)
2014-01-10 10:45:22,296 DEBUG END: running time==>136


2014-01-10 10:45:22,296 DEBUG START(List and Join)
2014-01-10 10:45:33,914 DEBUG END: running time==>11

== TEST Code ==

import datetime
import time
import sys
import logging
import codecs
import cStringIO, codecs


class StringOperation:

#using +
def run_concat_test_01(self):

for idx in range(1,1000):
src = "{}".format(idx)
for subidx in range(1,10000):
src = src + "AAAAA"
src = src + "BBBBB"
src = src + "BBBBB"
src = src + "CCCCC"
src = src + "한글 " # non romantic 

res = src # coppy other

#using writer
def run_concat_test_02(self):

for idx in range(1,1000):
source = cStringIO.StringIO()
wrapper = codecs.getwriter("utf8")(source)
wrapper.writelines("{}".format(idx))

for subidx in range(1,10000):
wrapper.writelines("AAAAA")
wrapper.writelines("BBBBB")
wrapper.writelines("BBBBB")
wrapper.writelines("CCCCC")
wrapper.writelines(u"한글") #non romantic

res = wrapper.getvalue() #copy res

source.close()


# using list + join
def run_concat_test_03(self):

for idx in range(1,1000):
source = []
source.append("{}".format(idx))

for subidx in range(1,10000):
source.append("AAAAA")
source.append("BBBBB")
source.append("BBBBB")
source.append("CCCCC")
source.append(u"한글") #non romantic

res = "".join(source)


def  run_test(self, title, func_idx):
funcs={
"plus": self.run_concat_test_01, 
"writer": self.run_concat_test_02, 
"list":self.run_concat_test_03}


st_time = datetime.datetime.now()
logging.debug("START({})".format(title))

funcs[func_idx]()

ed_time = datetime.datetime.now()
diff_time = ed_time - st_time
logging.debug("END: running time : {} seconds\r\n".format(diff_time.seconds))


today = datetime.date.today()
fname = "{0}{1}{2}-String_operation.log".format(today.year,today.month,today.day)
logging.basicConfig(filename=fname,level=logging.DEBUG, format='%(asctime)s %(levelname)s %(message)s')
logging.info("-------------------------------------")


oTester = StringOperation()

print "Plus Method"
oTester.run_test("Normal string Plus","plus")

print "Writer Method"
oTester.run_test("StreamWriter", "writer")

print "List Method"

oTester.run_test("List and Join","list")


== Running Environment==
windows 7  64bit
memory : 8G
pythone 2.7.3   (32bit)   # I used 32bit for compatibility



댓글 없음:

댓글 쓰기