Skip to content

Instantly share code, notes, and snippets.

@sgillies
Created August 29, 2012 18:05
Show Gist options
  • Save sgillies/3516360 to your computer and use it in GitHub Desktop.
Save sgillies/3516360 to your computer and use it in GitHub Desktop.
union-benchmark.py
from itertools import islice
import timeit
from shapely.geometry import Point
from shapely.ops import unary_union
def halton(base):
"""Returns an iterator over an infinite Halton sequence"""
def value(index):
result = 0.0
f = 1.0/base
i = index
while i > 0:
result += f * (i % base)
i = i/base
f = f/base
return result
i = 1
while i > 0:
yield value(i)
i += 1
coords = zip(
list(islice(halton(5), 20, 120)),
list(islice(halton(7), 20, 120)) )
patches = [Point(xy).buffer(0.05) for xy in coords]
N = 100 # num of vertices
M = 100 # num of passes
print
print "N=%s" % N
print "======"
print "Binary union"
s = "u = reduce(lambda a, b: a.union(b), patches[1:], patches[0])"
t = timeit.Timer(
stmt=s,
setup='from __main__ import patches'
)
print "%.2f usec/pass" % (1000000 * t.timeit(number=M)/M)
print
print "Unary union"
s = "u = unary_union(patches)"
t = timeit.Timer(
stmt=s,
setup='from __main__ import patches, unary_union'
)
print "%.2f usec/pass" % (1000000 * t.timeit(number=M)/M)
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment