json.dump(x,f) is much slower than f.write(json.dumps(x))
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 36k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
Bug report
Bug description:
Experimentally I measured a huge performance improvement when I switched my code from
json.dump(x, f, **)
to
f.write(json.dumps(x, **))
Method
I essentially wrote the same contents to different files sequentially and measured the total amount of time taken. The json contents had 1, 300, and 400 entries per level, and 1, 5, and 6 levels of depth. There's quite a level of variance here but this wasn't what I was trying to measure in the first place. I discovered this by chance, so forgive the lack of precision. I also don't have the source code anymore because I wasn't originally planning to report this discovery.
Results
| File Size | Consecutive Files | dump µs | dumps µs |
|---|---|---|---|
| 74 | 1 | 508 | 581 |
| 74 | 2 | 520 | 541 |
| 74 | 4 | 1153 | 1151 |
| 74 | 8 | 1930 | 1750 |
| 39184 | 1 | 6363 | 1086 |
| 39184 | 2 | 11261 | 1821 |
| 39184 | 4 | 38126 | 3521 |
| 39184 | 8 | 80411 | 6466 |
| 468218 | 1 | 82821 | 11921 |
| 468218 | 2 | 150234 | 38017 |
| 468218 | 4 | 302357 | 42137 |
| 468218 | 8 | 573450 | 78545 |
Conclusion
A cursory investigation into the cpython code suggests that the slow part is the sequential writing of the iterencode yield. The chunks are quite small.
CPython versions tested on:
3.10
Operating systems tested on:
macOS
Linked PRs
- gh-130076
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng việc xem xét PR gh-130076 được liên kết và mã JSON của CPython liên quan đến các lần ghi tuần tự từ iterencode. Tái hiện các phép đo thời gian dump-versus-dumps đã được báo cáo, sau đó xác minh rằng thay đổi này cải thiện hiệu năng dump đối với các trường hợp lớn hơn mà không thay đổi đầu ra.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100