Skip to content

Instantly share code, notes, and snippets.

Created February 8, 2016 16:46
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
import datetime
import itertools
import random
import sys
import timeit
from collections import defaultdict
import mongoengine as db
from pycallgraph.output.graphviz import GraphvizOutput
from pycallgraph.pycallgraph import PyCallGraph
class Data(db.EmbeddedDocument):
subf0 = db.ListField(db.IntField())
subf1 = db.ListField(db.IntField())
subf2 = db.ListField(db.IntField())
subf3 = db.ListField(db.IntField())
subf4 = db.ListField(db.IntField())
class MyDictModel(db.Document):
date = db.DateTimeField(required=True,
data_dict_1 = db.DictField()
class MyEmbedModel(db.Document):
date = db.DateTimeField(required=True,
data_dict_1 = db.EmbeddedDocumentField(Data)
data = ["subf{}".format(f) for f in range(5)]
m_dict = MyDictModel()
my_dict = dict([(d, list(random.sample(range(50000), 20000))) for d in data])
m_dict.data_dict_1 = my_dict
m_embed = MyEmbedModel()
my_data = Data()
for f in data:
my_data[f] = list(random.sample(range(50000), 20000))
m_embed.data_dict_1 = my_data
def pymongo_dict_doc():
return db.connection.get_connection()["test-dicts"]['my_dict_model'].find_one()
def pymongo_embed_doc():
return db.connection.get_connection()["test-dicts"]['my_embed_model'].find_one()
def mongoengine_dict_doc():
return MyDictModel.objects.first()
def mongoengine_embed_doc():
return MyEmbedModel.objects.first()
if __name__ == '__main__':
print("pymongo with dict took {:2.2f}s".format(timeit.timeit(pymongo_dict_doc, number=10)))
print("pymongo with embed took {:2.2f}s".format(timeit.timeit(pymongo_embed_doc, number=10)))
print("mongoengine with dict took", timeit.timeit(mongoengine_dict_doc, number=10))
print("mongoengine with embed took", timeit.timeit(mongoengine_embed_doc, number=10))
out1 = GraphvizOutput()
out1.output_file = "viz_embed.png"
out2 = GraphvizOutput()
out2.output_file = "viz_dict.png"
with PyCallGraph(output=out1):
with PyCallGraph(output=out2):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment