Skip to content

Instantly share code, notes, and snippets.

@paulocheque
Last active August 29, 2015 14:26
Show Gist options
  • Save paulocheque/036f3e849c9a5be994ca to your computer and use it in GitHub Desktop.
Save paulocheque/036f3e849c9a5be994ca to your computer and use it in GitHub Desktop.
import timeit
import random
def bench(stmt, setup, n=1000, repeat=10):
'''
Run `repeat` times the `timeit` with:
Run `n` times the `stmt`
'''
r = timeit.repeat(stmt, setup=setup, number=n, repeat=repeat)
print('%10s %.5f' % (stmt, min(r)))
locations = []
for i in range(100):
locations.append(dict(lat=random.randint(0, 200), long=random.randint(0, 200), timestamp=random.randint(10000, 15000)))
def a():
coordinates_timestamp = []
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
coordinates_timestamp.append(pos)
coordinates_timestamp = tuple(map(tuple, coordinates_timestamp))
coordinates_timestamp = set(coordinates_timestamp)
coordinates_timestamp = sorted(coordinates_timestamp, key=lambda t: t[2])
# print(coordinates_timestamp)
def a2():
coordinates_timestamp = []
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
coordinates_timestamp.append(pos)
coordinates_timestamp = tuple(coordinates_timestamp)
coordinates_timestamp = set(coordinates_timestamp)
coordinates_timestamp = sorted(coordinates_timestamp, key=lambda t: t[2])
# print(coordinates_timestamp)
def a3():
coordinates_timestamp = []
for location in locations:
lat = float(location['lat'])
lon = float(location['long'])
timestamp = int(location['timestamp'])
pos = (lon, lat, timestamp)
coordinates_timestamp.append(pos)
coordinates_timestamp = tuple(coordinates_timestamp)
coordinates_timestamp = list(set(coordinates_timestamp))
coordinates_timestamp.sort(key=lambda t: t[2])
# print(coordinates_timestamp)
def a4():
coordinates_timestamp = (
# (float(l.get('long')), float(l.get('lat')), int(l.get('timestamp', l.get('t'))))
(float(l['long']), float(l['lat']), int(l['timestamp']))
for l in locations
)
coordinates_timestamp = list(set(coordinates_timestamp))
coordinates_timestamp.sort(key=lambda t: t[2])
# print(coordinates_timestamp)
def b():
coordinates_timestamp = ()
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
coordinates_timestamp = coordinates_timestamp + (pos,)
coordinates_timestamp = set(coordinates_timestamp)
coordinates_timestamp = sorted(coordinates_timestamp, key=lambda t: t[2])
# print(coordinates_timestamp)
def b2():
coordinates_timestamp = ()
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
coordinates_timestamp = coordinates_timestamp + (pos,)
coordinates_timestamp = list(set(coordinates_timestamp))
coordinates_timestamp.sort(key=lambda t: t[2])
# print(coordinates_timestamp)
def c():
coordinates_timestamp = []
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
coordinates_timestamp.append(pos)
coordinates_timestamp = tuple(coordinates_timestamp)
coordinates_timestamp = set(coordinates_timestamp)
def d():
coordinates_timestamp = []
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
coordinates_timestamp.append(pos)
coordinates_timestamp = tuple(coordinates_timestamp)
def e():
coordinates_timestamp = []
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
coordinates_timestamp.append(pos)
def f():
coordinates_timestamp = []
for location in locations:
lat = float(location.get('lat'))
lon = float(location.get('long'))
timestamp = int(location.get('timestamp', location.get('t')))
pos = (lon, lat, timestamp)
if __name__ == "__main__":
setup = 'from __main__ import a, b, c, d, e, a2, b2, a3, a4'
print('Testing functions')
bench("a()", setup)
bench("a2()", setup)
bench("a3()", setup)
bench("a4()", setup)
bench("b()", setup)
bench("b2()", setup)
bench("c()", setup)
bench("d()", setup)
bench("e()", setup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment