- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10
Closed
Description
loads benchmark uses following logic:
    compare("bsonjs.loads(j)",
            "bson.BSON().encode(json_util.loads(j))",
            iterations,
            setup)- Comparing bsonjs.loads(j)tobson.BSON().encode(json_util.loads(j))is unfair.bson.BSON().encodeis supposed to encode pythondicts, so we should pass it adictdirectly instead of loading from json before passing.
# Before
bson.BSON().encode(json_util.loads(j))
# After
bson.BSON().encode(doc)- Using bson.BSON().encodeis not recommeded, because of its additional performance cost, benchmark should usebson.encodemethod directly:
 
# Before
bson.BSON().encode(doc)
# After
bson.encode(doc)Final code:
# loads
compare('bsonjs.loads(j)', 'bson.encode(doc)', iterations, setup)Test results
If we run benchmark with fixed code we get following results:
Mac M3
Timing: bsonjs.dumps(b)
10000 loops, best of 3: 0.03492570900000001
Timing: json_util.dumps(bson.decode(b))
10000 loops, best of 3: 0.2038257080000001
bsonjs is 5.84x faster than json_util
Timing: bsonjs.loads(j)
10000 loops, best of 3: 0.03936475000000006
Timing: bson.encode(doc)
10000 loops, best of 3: 0.014574333999999967
bsonjs is 0.37x faster than json_utilMac Intel
Timing: bsonjs.dumps(b)
10000 loops, best of 3: 0.08610098099999997
Timing: json_util.dumps(bson.decode(b))
10000 loops, best of 3: 0.6389648219999999
bsonjs is 7.42x faster than json_util
Timing: bsonjs.loads(j)
10000 loops, best of 3: 0.08243793199999949
Timing: bson.encode(doc)
10000 loops, best of 3: 0.035725252999999846
bsonjs is 0.43x faster than json_utilSummary
Looks like in reality bsonjs.loads is more than 2 times slower than bson.encode.
andriy-lytvyn
Metadata
Metadata
Assignees
Labels
No labels