Advertisement
Guest User

Untitled

a guest
Oct 7th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from __future__ import absolute_import, division, print_function, unicode_literals
  5.  
  6. import random
  7. import math
  8. import time
  9.  
  10. def main():
  11.     arr_size = 100000
  12.     # generate
  13.     arr = []
  14.     for i in xrange(0, arr_size):
  15.         #arr.append(round(random.gauss(5, 0.85), 0))
  16.         arr.append(i // 16)
  17.     arr.sort()
  18.     #print('array:')
  19.     #print(arr)
  20.  
  21.     # O(n)
  22.     t1 = time.time()
  23.     frq0 = {}
  24.     for i in arr:
  25.         if i in frq0:
  26.             frq0[i] += 1
  27.         else:
  28.             frq0[i] = 1
  29.     t2 = time.time()
  30.     print('frq O(n):')
  31.     #print(sorted(frq0.iteritems()))
  32.     print('length: %d' % len(frq0))
  33.     print('time: %.3f' % (t2 - t1))
  34.  
  35.     ## O(sqrt(n))
  36.     #t1 = time.time()
  37.     #frq = {}
  38.     #left_k = 0
  39.     #while left_k < arr_size:
  40.     #    left_v = arr[left_k]
  41.     #    width = arr_size - 1 - left_k
  42.     #    step = int(math.sqrt(width))
  43.     #    #step = rsqrt(width)
  44.     #    step = step if step else 1
  45.     #    big_probe = left_k
  46.     #    big_probe += step
  47.     #    while big_probe < arr_size:
  48.     #        if arr[big_probe] != left_v:
  49.     #            break
  50.     #        big_probe += step
  51.     #    small_probe = big_probe - step
  52.     #    while small_probe < arr_size:
  53.     #        if arr[small_probe] != left_v:
  54.     #            break
  55.     #        small_probe += 1
  56.     #    frq[left_v] = (small_probe - left_k)
  57.     #    if left_k == small_probe:
  58.     #        print('// hm...')
  59.     #        break
  60.     #    left_k = small_probe
  61.     #t2 = time.time()
  62.     #print('frq O(sqrt(n)):')
  63.     #print(sorted(frq.iteritems()))
  64.     #print('time: %.3f' % (t2 - t1))
  65.  
  66.     ## O(хер_знает_солько)
  67.     t1 = time.time()
  68.     frq = {}
  69.     left_k = 0
  70.     while left_k < arr_size:
  71.         left_v = arr[left_k]
  72.         step = 1
  73.         probe = left_k + step
  74.         while True:
  75.             if step == 1:
  76.                 if probe == arr_size or arr[probe] != left_v:
  77.                     break
  78.             if probe >= arr_size or arr[probe] != left_v:
  79.                 probe -= step
  80.                 step = 1
  81.                 probe += step
  82.                 continue
  83.             step *= 2
  84.             probe += step
  85.         frq[left_v] = probe - left_k
  86.         left_k = probe
  87.     t2 = time.time()
  88.     print('frq O(хер_знает_сколько):')
  89.     #print(sorted(frq.iteritems()))
  90.     print('time: %.3f' % (t2 - t1))
  91.  
  92. if __name__ == '__main__':
  93.     main()
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement