Advertisement
Guest User

Untitled

a guest
Apr 5th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.39 KB | None | 0 0
  1. #!/usr/bin/env lua
  2. MAX_RANGE = 1000000
  3.  
  4. function test_dict()
  5.     --[2012-02-04] set 2.11s get 0.87s clear 0.19s
  6.     io.write("---- test_dict -----\n")
  7.     local i = 0
  8.     local d = {}
  9.     local s
  10.     local start
  11.     start = os.clock()
  12.     io.write(string.format("test dict set (%d)...\t", MAX_RANGE))
  13.     for i=0, MAX_RANGE do
  14.         s = string.format("%d", i)
  15.         --print(s)
  16.         d[s] = s
  17.     end
  18.     io.write(string.format("%.2fs\n", os.clock()-start))
  19.     start = os.clock()
  20.     io.write(string.format("test dict get (%d)...\t", MAX_RANGE))
  21.     for i=0, MAX_RANGE do
  22.         s = d[string.format("%d", i)]
  23.     end
  24.     io.write(string.format("%.2fs\n", os.clock()-start))
  25.     start = os.clock()
  26.     io.write(string.format("test dict clear (%d)...\t", MAX_RANGE))
  27.     for i=0, MAX_RANGE do
  28.         table.remove(d, i)
  29.     end
  30.     io.write(string.format("%.2fs\n", os.clock()-start))
  31.     io.write("\n")
  32. end
  33.  
  34. function test_list()
  35.     --[2012-02-04] set 0.33s get 0.03s each 0.09s
  36.     io.write("---- test_list -----\n")
  37.     local i = 0
  38.     local j
  39.     local l = {}
  40.     local s
  41.     local start
  42.     start = os.clock()
  43.     io.write(string.format("test list set (%d)...\t", MAX_RANGE))
  44.     for i=0, MAX_RANGE do
  45.         table.insert(l, i)
  46.     end
  47.     io.write(string.format("%.2fs\n", os.clock()-start))
  48.     start = os.clock()
  49.     io.write(string.format("test list get (%d)...\t", MAX_RANGE))
  50.     for i=0, MAX_RANGE do
  51.         j = l[i]
  52.     end
  53.     io.write(string.format("%.2fs\n", os.clock()-start))
  54.     start = os.clock()
  55.     io.write(string.format("test list each (%d)...\t", MAX_RANGE))
  56.     for i, j in pairs(l) do
  57.     end
  58.     io.write(string.format("%.2fs\n", os.clock()-start))
  59.     io.write("\n")
  60. end
  61.  
  62. function test_str()
  63.     --[2012-02-05] find 0.94s replace 1.10s
  64.     io.write("---- test_str -----\n")
  65.     local i = 0
  66.     local s = "1143534=69789+35435^(100&10+5)"
  67.     local rep_before = "35"
  68.     local rep_after = "3535"
  69.     start = os.clock()
  70.     io.write(string.format("test str find (%d)...\t", MAX_RANGE))
  71.     for i=0, MAX_RANGE do
  72.         string.find(s, string.format("%d", i))
  73.     end
  74.     io.write(string.format("%.2fs\n", os.clock()-start))
  75.     --s = string.gsub(s, rep_before, rep_after)
  76.     --print(s)
  77.     start = os.clock()
  78.     io.write(string.format("test str replace (%d)...\t", MAX_RANGE))
  79.     for i=MAX_RANGE/2, MAX_RANGE do
  80.         s = string.gsub(s, rep_before, rep_after)
  81.         s = string.gsub(s, rep_after, rep_before)
  82.     end
  83.     io.write(string.format("%.2fs\n", os.clock()-start))
  84.     io.write("\n")
  85. end
  86.  
  87. if not package.loaded[...] then
  88.     test_dict()
  89.     test_list()
  90.     test_str()
  91. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement