Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 5th, 2012  |  syntax: Python  |  size: 1.81 KB  |  views: 76  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import sys
  4. import os
  5. import time
  6. sys.stdout = os.fdopen(sys.stdout.fileno(), "wb", 0) #-u
  7. MAX_RANGE = 1000000
  8.  
  9. def test_dict():
  10.         #[2012-02-01] set 1.05s get 0.75s clear 0.11s
  11.         print "---- test_dict -----"
  12.         start = time.time()
  13.         print "test dict set (%d)...\t"%MAX_RANGE,
  14.         d = {}
  15.         for i in xrange(MAX_RANGE):
  16.                 s = str(i)
  17.                 d[s] = s
  18.         print "%.2fs"%(time.time()-start)
  19.         start = time.time()
  20.         print "test dict get (%d)...\t"%MAX_RANGE,
  21.         for i in xrange(MAX_RANGE):
  22.                 s = d[str(i)]
  23.         print "%.2fs"%(time.time()-start)
  24.         start = time.time()
  25.         print "test dict clear (%d)...\t"%MAX_RANGE,
  26.         d.clear()
  27.         print "%.2fs"%(time.time()-start)
  28.         print
  29.  
  30. def test_list():
  31.         #[2012-02-04] set 0.02s get 0.09s each 0.09s
  32.         start = time.time()
  33.         print "---- test_list -----"
  34.         print "test list set (%d)...\t"%MAX_RANGE,
  35.         l = range(MAX_RANGE)
  36.         print "%.2fs"%(time.time()-start)
  37.         start = time.time()
  38.         print "test list get (%d)...\t"%MAX_RANGE,
  39.         for i in xrange(MAX_RANGE):
  40.                 l[i]
  41.         print "%.2fs"%(time.time()-start)
  42.         start = time.time()
  43.         print "test list each (%d)...\t"%MAX_RANGE,
  44.         for i in l: i+=1 #no effect
  45.         print "%.2fs"%(time.time()-start)
  46.         print
  47.  
  48. def test_str():
  49.         #[2012-02-05] find 0.94s replace 0.67s
  50.         print "---- test_str -----"
  51.         string = "1143534=69789+35435^(100&10+5)"
  52.         rep_before = "35"
  53.         rep_after = "3535"
  54.         start = time.time()
  55.         print "test str find (%d)...\t"%MAX_RANGE,
  56.         for i in xrange(MAX_RANGE):
  57.                 string.find(str(i))
  58.         print "%.2fs"%(time.time()-start)
  59.         start = time.time()
  60.         print "test str replace (%d)...\t"%MAX_RANGE,
  61.         for i in xrange(MAX_RANGE/2):
  62.                 string = string.replace(rep_before, rep_after)
  63.                 string = string.replace(rep_after, rep_before)
  64.         print "%.2fs"%(time.time()-start)
  65.         print
  66.  
  67. if __name__ == "__main__":
  68.         test_dict()
  69.         test_list()
  70.         test_str()