root/Rtree/trunk/tests/benchmarks.py

Revision 1457, 3.6 KB (checked in by brentp, 5 months ago)

allow the user to create a subclass which specifies an attribute 'pickle_protocol' which is then sent to the dumps() command.
using pickle_protocol = -1 (see tests/benchmarks.py) gives a speed improvement of about 40% for loading and querying data.

#################### BEFORE ####################################

Stream load:
234491.55 usec/pass

One-at-a-time load:
502666.90 usec/pass

30000 points
Query box: (1240000, 1010000, 1400000, 1390000)

Brute Force:
44 hits
16955.10 usec/pass

Memory-based Rtree Intersection:
44 hits
5147.98 usec/pass

Disk-based Rtree Intersection:
44 hits
5193.56 usec/pass

Disk-based Rtree Intersection without Item() wrapper (objects='raw'):
44 raw hits
2500.45 usec/pass

#################### AFTER ####################################
# with pickle_protocol = -1
#################### AFTER ####################################

Stream load:
170872.20 usec/pass

One-at-a-time load:
348162.10 usec/pass

30000 points
Query box: (1240000, 1010000, 1400000, 1390000)

Brute Force:
48 hits
16882.21 usec/pass

Memory-based Rtree Intersection:
48 hits
4365.00 usec/pass

Disk-based Rtree Intersection:
48 hits
4408.85 usec/pass

Disk-based Rtree Intersection without Item() wrapper (objects='raw'):
48 raw hits
1546.85 usec/pass

  • Property svn:eol-style set to native
Line 
1# Brute Force:
2# 481 hits
3# 29637.96 usec/pass
4#
5# Memory-based Rtree Intersection:
6# 481 hits
7# 1216.70 usec/pass
8# \Disk-based Rtree Intersection:
9# 481 hits
10# 2617.95 usec/pass
11
12import random
13import timeit
14
15try:
16    import pkg_resources
17    pkg_resources.require('Rtree')
18except:
19    pass
20
21from rtree import Rtree as _Rtree
22
23TEST_TIMES = 20
24
25# a very basic Geometry
26class Point(object):
27    def __init__(self, x, y):
28        self.x = x
29        self.y = y
30
31# Scatter points randomly in a 1x1 box
32#
33
34class Rtree(_Rtree):
35    pickle_protocol = -1
36
37bounds = (0, 0, 6000000, 6000000)
38count = 30000
39points = []
40
41insert_object = None
42insert_object = {'a': range(100), 'b': 10, 'c': object(), 'd': dict(x=1), 'e': Point(2, 3)}
43
44index = Rtree()
45disk_index = Rtree('test', overwrite=1)
46
47coordinates = []
48for i in xrange(count):
49    x = random.randrange(bounds[0], bounds[2]) + random.random()
50    y = random.randrange(bounds[1], bounds[3]) + random.random()
51    point = Point(x, y)
52    points.append(point)
53
54    index.add(i, (x, y), insert_object)
55    disk_index.add(i, (x, y), insert_object)
56    coordinates.append((i, (x, y, x, y), insert_object))
57
58s ="""
59bulk = Rtree(coordinates[:2000])
60"""
61t = timeit.Timer(stmt=s, setup='from __main__ import coordinates, Rtree, insert_object')
62print "\nStream load:"
63print "%.2f usec/pass" % (1000000 * t.timeit(number=TEST_TIMES)/TEST_TIMES)
64
65s ="""
66idx = Rtree()
67i = 0
68for point in points[:2000]:
69    idx.add(i, (point.x, point.y), insert_object)
70    i+=1
71"""
72t = timeit.Timer(stmt=s, setup='from __main__ import points, Rtree, insert_object')
73print "\nOne-at-a-time load:"
74print "%.2f usec/pass\n\n" % (1000000 * t.timeit(number=TEST_TIMES)/TEST_TIMES)
75
76
77bbox = (1240000, 1010000, 1400000, 1390000)
78print count, "points"
79print "Query box: ", bbox
80print ""
81
82# Brute force all points within a 0.1x0.1 box
83s = """
84hits = [p for p in points if p.x >= bbox[0] and p.x <= bbox[2] and p.y >= bbox[1] and p.y <= bbox[3]]
85"""
86t = timeit.Timer(stmt=s, setup='from __main__ import points, bbox')
87print "\nBrute Force:"
88print len([p for p in points if p.x >= bbox[0] and p.x <= bbox[2] and p.y >= bbox[1] and p.y <= bbox[3]]), "hits"
89print "%.2f usec/pass" % (1000000 * t.timeit(number=TEST_TIMES)/TEST_TIMES)
90
91# 0.1x0.1 box using intersection
92
93if insert_object is None:
94    s = """
95    hits = [points[id] for id in index.intersection(bbox)]
96    """
97else:
98    s = """
99    hits = [p.object for p in index.intersection(bbox, objects=insert_object)]
100    """
101
102t = timeit.Timer(stmt=s, setup='from __main__ import points, index, bbox, insert_object')
103print "\nMemory-based Rtree Intersection:"
104print len([points[id] for id in index.intersection(bbox)]), "hits"
105print "%.2f usec/pass" % (1000000 * t.timeit(number=100)/100)
106
107
108# run same test on disk_index.
109s = s.replace("index.", "disk_index.")
110
111t = timeit.Timer(stmt=s, setup='from __main__ import points, disk_index, bbox, insert_object')
112print "\nDisk-based Rtree Intersection:"
113print len(disk_index.intersection(bbox)), "hits"
114print "%.2f usec/pass" % (1000000 * t.timeit(number=TEST_TIMES)/TEST_TIMES)
115
116
117if insert_object:
118    s = """
119        hits = disk_index.intersection(bbox, objects="raw")
120        """
121    t = timeit.Timer(stmt=s, setup='from __main__ import points, disk_index, bbox, insert_object')
122    print "\nDisk-based Rtree Intersection without Item() wrapper (objects='raw'):"
123    result = disk_index.intersection(bbox, objects="raw")
124    print len(result), "raw hits"
125    print "%.2f usec/pass" % (1000000 * t.timeit(number=TEST_TIMES)/TEST_TIMES)
126    assert 'a' in result[0], result[0]
127
128import os
129try:
130    os.remove('test.dat')
131    os.remove('test.idx')
132except:
133    pass
Note: See TracBrowser for help on using the browser.