Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Why Python's deepcopy() is surprisingly slow (and better alternatives) (codeflash.ai)
6 points by misrasaurabh1 49 days ago | hide | past | favorite | 1 comment


I've been running into performance bottlenecks in the wild where `copy.deepcopy()` was the bottleneck. After digging into it, I discovered that deepcopy can actually be slower than even serializing and deserializing with pickle or json in many cases!

I wrote up my findings on why this happens and some practical alternatives that can give you significant performance improvements.

*TL;DR:* deepcopy's recursive approach and safety checks create memory overhead that often isn't worth it. The post covers when to use alternatives like shallow copy + manual handling, pickle round-trips, or restructuring your code to avoid copying altogether.

Has anyone else run into this? Curious to hear about other performance gotchas you've discovered in commonly-used Python functions.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: