daily pastebin goal
74%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from cStringIO import StringIO
  2. from timeit import Timer
  3. import commands, os
  4. from sys import argv
  5.  
  6. def method1():
  7.     global z
  8.     out_str = ''
  9.     for num in xrange(loop_count):
  10.         out_str += `num`
  11.     ps_stats()
  12.     z = out_str
  13.  
  14. def method2():
  15.     global z
  16.     from array import array
  17.     char_array = array('c')
  18.     for num in xrange(loop_count):
  19.         char_array.fromstring(`num`)
  20.     ps_stats()
  21.     z = char_array.tostring()
  22.  
  23. def method3():
  24.     global z
  25.     str_list = []
  26.     for num in xrange(loop_count):
  27.         str_list.append(`num`)
  28.     out_str = ''.join(str_list)
  29.     ps_stats()
  30.     z = out_str
  31.  
  32. def method4():
  33.     global z
  34.     file_str = StringIO()
  35.     for num in xrange(loop_count):
  36.         file_str.write(`num`)
  37.     out_str = file_str.getvalue()
  38.     ps_stats()
  39.     z = out_str
  40.  
  41. def method5():
  42.     global z
  43.     out_str = ''.join([`num` for num in xrange(loop_count)])
  44.     ps_stats()
  45.     z = out_str
  46.  
  47. def method6():
  48.     global z
  49.     out_str = ''.join(`num` for num in xrange(loop_count))
  50.     ps_stats()
  51.     z = out_str
  52.  
  53. def ps_stats():
  54.     global process_size
  55.     ps = commands.getoutput('ps up ' + `pid`)
  56.     process_size = ps.split()[16]
  57.  
  58. def call_method():
  59.     global process_size
  60.     global num
  61.     global z
  62.     t = Timer(
  63.         'z = eval(\'method\' + str(num))()',
  64.         "from __main__ import method" + str(num)  + ", num, z")
  65.     print "time: %.2f usec/pass" % (1000000 * t.timeit(number=100)/100)
  66.     print "method", num
  67.     print "output size ", len(z) / 1024, "kb"
  68.     print "process size", process_size, "kb"
  69.     print
  70.  
  71. loop_count = 100000
  72. pid = os.getpid()
  73. num = 1
  74. z = ''
  75.  
  76. for i in xrange(6):
  77.     num = i+1
  78.     call_method()
  79.  
  80.  
  81. """
  82. time: 59436.04 usec/pass
  83. method 1
  84. output size  477 kb
  85. process size 5888 kb
  86.  
  87. time: 75490.27 usec/pass
  88. method 2
  89. output size  477 kb
  90. process size 6904 kb
  91.  
  92. time: 57083.68 usec/pass
  93. method 3
  94. output size  477 kb
  95. process size 13784 kb
  96.  
  97. time: 67105.47 usec/pass
  98. method 4
  99. output size  477 kb
  100. process size 6664 kb
  101.  
  102. time: 44640.27 usec/pass
  103. method 5
  104. output size  477 kb
  105. process size 13560 kb
  106.  
  107. time: 51163.65 usec/pass
  108. method 6
  109. output size  477 kb
  110. process size 13252 kb
  111. """
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top