Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: Python,bench,chk,test_zip,test_izip,test_for,test_for_slice, Created At: 10/24/2016 8:26:45 PMViews:

HTML view:
Copy Source | Copy HTML
  1. def bench():
  2.     import sys
  3.     from time import time
  4.     from itertools import izip, islice
  5.  
  6.     iterations = range(150)
  7.     x = range(100000)
  8.  
  9.     def chk(name, func):
  10.         t = time()
  11.         for i in iterations:
  12.             result = func(x)
  13.         res = 'ok' if tuple(result) == (x[-2], x[-1]) else '!!'
  14.         print '[%s] %10s: %0.4f' % (res, name, time()-t)
  15.         if res == '!!':
  16.             print 'auch!'
  17.             print (x[-2], x[-1])
  18.             print result
  19.         sys.stdout.flush()
  20.  
  21.     def test_zip(d):
  22.         for (val1, val2) in zip(d[::2], d[1::2]):
  23.             res = val1, val2
  24.         return res
  25.  
  26.     def test_izip(d):
  27.         for (val1, val2) in izip(islice(d, 0, None, 2), islice(d, 1, None, 2)):
  28.             res = val1, val2
  29.         return res
  30.  
  31.     def test_for(d):
  32.         for ix in range(0, len(x), 2):
  33.             res = x[ix], x[ix+1]
  34.         return res
  35.  
  36.     def test_for_slice(d):
  37.         for ix in range(0, len(x), 2):
  38.             res = x[ix:ix+2]
  39.         return res
  40.  
  41.     def test_ifor(d):
  42.         for ix in xrange(0, len(x), 2):
  43.             res = x[ix], x[ix+1]
  44.         return res
  45.  
  46.     def test_for_enum(d):
  47.         for idx, val1 in enumerate(x[::2]):
  48.             res = val1, x[idx*2+1]
  49.         return res
  50.  
  51.     def test_for_count(d):
  52.         idx = -1
  53.         for val1 in x[::2]:
  54.             idx += 2
  55.             res = val1, x[idx]
  56.         return res
  57.  
  58.     def test_for_islice(d):
  59.         idx = -1
  60.         for val1 in islice(x, 0, None, 2):
  61.             idx += 2
  62.             res = val1, x[idx]
  63.         return res
  64.  
  65.     chk('zip', test_zip)
  66.     chk('izip', test_izip)
  67.     chk('for', test_for)
  68.     chk('for+slice', test_for_slice)
  69.     chk('ifor', test_ifor)
  70.     chk('for+enum', test_for_enum)
  71.     chk('for+cnt', test_for_count)
  72.     chk('for+islice', test_for_islice)

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2017