Skip to content

Instantly share code, notes, and snippets.

@msztolcman
Created January 31, 2013 17:03
Show Gist options
  • Save msztolcman/4684374 to your computer and use it in GitHub Desktop.
Save msztolcman/4684374 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python -tt
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import os, os.path
import sys
import re
from pprint import pprint, pformat
import timeit
x1 = range (1000000)
x2 = range (0, 2000000, 2)
assert len (x1) == len (x2), 'error'
from itertools import izip
class T:
@staticmethod
def with_zip ():
for i, j in zip (x1, x2):
k = i * j
@staticmethod
def with_izip ():
for i, j in izip (x1, x2):
k = i * j
@staticmethod
def without_zip_range ():
for i in range (len (x1)):
k = x1[i] * x2[i]
@staticmethod
def without_zip_xrange ():
for i in xrange (len (x1)):
k = x1[i] * x2[i]
for i in ('with_zip', 'with_izip', 'without_zip_range', 'without_zip_xrange'):
print (i, timeit.timeit (getattr (T, i), number=100))
# results:
# % python ~/p/python/281.py
# with_zip 37.5300300121
# with_izip 14.6356389523
# without_zip_range 21.2632069588
# without_zip_xrange 17.0181829929
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment