- Python के computation, memory और I/O performance metrics को व्यवस्थित रूप से मापने वाले benchmark नतीजे, जिनमें हर operation का समय और memory usage मात्रात्मक रूप से दिखाया गया है
- स्पीड के लिहाज़ से property access 14ns, list append 29ns, file open 9μs, FastAPI response 8.6μs जैसे विभिन्न operations की relative latency दी गई है
- मेमोरी के लिहाज़ से empty string 41 bytes, integer 28 bytes, empty list 56 bytes, empty dictionary 64 bytes, empty process 16MB जैसी ठोस संख्याएँ दी गई हैं
- data structures, serialization, async processing आदि अलग-अलग क्षेत्रों में standard library और alternative libraries (
orjson, msgspec आदि) के performance अंतर की तुलना की गई है
- मुख्य सीख के रूप में Python objects का ऊँचा memory overhead, dict/set lookup की तेज़ी,
__slots__ के memory-saving प्रभाव, और async processing के overhead को समझने की ज़रूरत पर ज़ोर दिया गया है
अवलोकन
- Python developers के लिए जानने लायक performance indicators को संकलित करने वाली सामग्री, जिसमें operation speed और memory usage को वास्तविक मापों के साथ दिखाया गया है
- benchmark CPython 3.14.2, Mac Mini M4 Pro (ARM, 14-core, 24GB RAM) वातावरण में चलाया गया
- नतीजे relative comparison पर केंद्रित हैं, और code व data GitHub repository में सार्वजनिक हैं
मेमोरी उपयोग (Memory Costs)
- खाली Python process 15.73MB memory उपयोग करता है
- strings का base size 41 bytes है, और हर अतिरिक्त character पर 1 byte बढ़ता है
- उदाहरण: empty string 41B, 100-character string 141B
- numeric types में small integers (0–256) 28B, large integer (1000) भी 28B, बहुत बड़ा integer (10ⁱ⁰⁰) 72B, floating point 24B
- collections का base size: list 56B, dictionary 64B, set 216B
- 1,000 items होने पर list 35.2KB, dictionary 63.4KB, set 59.6KB
- class instances: सामान्य class (5 attributes) 694B,
__slots__ class 212B
- 1,000 instances पर सामान्य class 165.2KB,
__slots__ class 79.1KB
बुनियादी operations (Basic Operations)
- arithmetic operations: integer addition 19ns, float addition 18.4ns, integer multiplication 19.4ns
- string operations: concatenation 39.1ns, f-string 64.9ns,
.format() 103ns, % formatting 89.8ns
- list operations:
append() 28.7ns, list comprehension (1,000 items) 9.45μs, वही काम for loop से 11.9μs
- list comprehension, for loop से लगभग 26% तेज़ है
collection access और iteration
- key/index access: dictionary lookup 21.9ns, set membership 19ns, list index access 17.6ns
- list membership (1,000 items) 3.85μs है, जो set/dictionary से लगभग 200 गुना धीमा है
- length check:
len() list पर 18.8ns, dictionary पर 17.6ns, set पर 18ns
- iteration: list (1,000 items) 7.87μs, dictionary 8.74μs,
sum() 1.87μs
class और attributes
- attribute access speed: सामान्य class और
__slots__ class दोनों में read 14.1ns, write लगभग 16ns
- अन्य operations:
@property read 19ns, getattr() 13.8ns, hasattr() 23.8ns
__slots__ इस्तेमाल करने पर memory saving 2 गुना से अधिक है, जबकि access speed लगभग समान रहती है
JSON और serialization
- standard library की तुलना में alternative libraries का performance
- complex objects को serialize करते समय
orjson 310ns पर, json के 2.65μs से 8 गुना से अधिक तेज़ है
msgspec 445ns, ujson 1.64μs
- deserialization में भी
orjson 839ns के साथ सबसे तेज़ है
- Pydantic:
model_dump_json() 1.54μs, model_validate_json() 2.99μs
web frameworks
- समान JSON response के आधार पर FastAPI 8.63μs, Starlette 8.01μs, Litestar 8.19μs, Flask 16.5μs, Django 18.1μs
- FastAPI की response speed, Django से लगभग 2 गुना तेज़ है
file I/O
- file open/close 9.05μs, 1KB read 10μs, 1MB read 33.6μs
- write: 1KB 35.1μs, 1MB 207μs
- Pickle,
json की तुलना में serialization और deserialization दोनों में लगभग 2 गुना तेज़ है (pickle.dumps() 1.3μs, json.dumps() 2.72μs)
database और cache
- SQLite: insert 192μs, select 3.57μs, update 5.22μs
- diskcache: set 23.9μs, get 4.25μs
- MongoDB: insert 119μs, find_one 121μs
- read speed में SQLite सबसे तेज़ है, जबकि diskcache की write performance बेहतर है
function calls और exceptions
- function call: empty function 22.4ns, method 23.3ns, lambda 19.7ns
- exception handling: try/except (normal path) 21.5ns, exception होने पर 139ns
- type checking:
isinstance() 18.3ns, type() comparison 21.8ns
async overhead
- coroutine creation 47ns,
run_until_complete 27.6μs
asyncio.sleep(0) 39.4μs, gather(10 coroutines) 55μs
- synchronous function call (20ns) की तुलना में async execution (28μs) लगभग 1,000 गुना धीमा है
मुख्य सीख (Key Takeaways)
- Python objects का memory overhead बड़ा होता है; खाली list भी 56 bytes लेती है
- dictionary और set lookup list search से सैकड़ों गुना तेज़ हैं
orjson, msgspec जैसी alternative JSON libraries standard से 3–8 गुना तेज़ हैं
- async processing का overhead बड़ा है, इसलिए इसका उपयोग केवल तब करना चाहिए जब parallelism की वास्तव में ज़रूरत हो
__slots__ मेमोरी को आधे से भी कम कर सकता है, और performance loss लगभग नहीं होता
अभी कोई टिप्पणी नहीं है.