Fundamentals 4 min read

Understanding Monkey Patching in Python: Concepts, Example, and Performance Boost

This article explains what monkey patching is in Python, demonstrates how to replace the standard json module with the faster ujson module using a simple runtime patch, and shows performance measurements before and after the patch to illustrate the speed improvement.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding Monkey Patching in Python: Concepts, Example, and Performance Boost

Monkey patch (also called monkey patching) is a technique that allows dynamic replacement of attributes or functions in a running program, effectively adding or modifying functionality at runtime.

In Python, monkey patching enables you to modify a class or module while the program is executing.

Example: a game developer originally imported the built‑in json module many times and later discovered that ujson is much faster. Instead of changing every import, a single monkey patch at program start can replace json.dumps with ujson.dumps .

Below is a helper module that defines a timing decorator and a json_dumps function using the standard json library:

"""
file:json_serialize.py
"""
import time
import json

# time test decorator
def run_time(func):
    def inner(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()
        print(f'程序用时:{end_time - start_time}')
        return result
    return inner

@run_time
def json_dumps(obj):
    return json.dumps(obj)

# generate test dict
test_dict = {i: 1 for i in range(1, 10000001)}

Running the original program:

"""
file:run.py
"""
from json_serialize import json_dumps, test_dict

print(f'json.dumps编码用时:', end='')
r1 = json_dumps(test_dict)

The original implementation is slow because the standard json encoder is not optimized for large data.

After applying a monkey patch that swaps the json.dumps function with ujson.dumps :

"""
file:run.py
"""
import json
import ujson
from json_serialize import json_dumps, test_dict

def monkey_patch_json():
    json.dumps = ujson.dumps

monkey_patch_json()
print(f'使用猴子补丁之后json.dumps编码用时:', end='')
json_dumps(test_dict)

Now the json module in the whole process has been replaced by the faster ujson implementation, resulting in a noticeable performance improvement.

performancedecoratormonkey-patchingujson
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.