Python
14 Dec 2011
 
 
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import pymongo
import time
import re
db = pymongo.Connection()['barn']
def test_mongo_logic():
start = time.time()
count = 0
total = 0
for item in db.cache.find({'$where': 'function(){return this.url.search(/10/) > -1}'}):
count += 1
if count == 10000:
break
print item['_id'], total
print '%.2f' % (time.time() - start)
def test_python_logic():
start = time.time()
count = 0
total = 0
rex = re.compile(r'10')
for item in db.cache.find({}):
if rex.search(item['url']):
count += 1
if count == 10000:
break
print item['_id'], total
print '%.2f' % (time.time() - start)
if __name__ == '__main__':
test_mongo_logic()
test_python_logic()