Attachment 'shufflemeter.py'

Download

   1 #!/usr/bin/env python
   2 # coding: utf
   3 '''
   4   1. Измеритель скорости. С помощью `time.time()` замерить, сколько операций `random.shuffle()` выполняет компьютер за 5 секунд
   5    * Написать собственную процедуру  `shuffle()`. Насколько она медленнее?
   6 '''
   7 
   8 import random, time
   9 
  10 def shuffle1(S):
  11     return [random.choice(S) for i in xrange(len(S))]
  12 
  13 def shuffle2(S):
  14     return [S.pop(random.randrange(len(S))) for i in xrange(len(S))]
  15 
  16 def test(S,F,T):
  17     t, n = time.time()+T, 0
  18     while time.time()<t:
  19         F(S[:])
  20         n+=1
  21     return n
  22 
  23 T=5
  24 
  25 for i in xrange(2):
  26     L=random.sample(xrange(1000),1000)
  27     for f in random.shuffle, shuffle1, shuffle2:
  28         print f, test(L,f,T)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.