Created
August 29, 2012 18:05
-
-
Save sgillies/3516360 to your computer and use it in GitHub Desktop.
union-benchmark.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "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 "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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment