Benchmark memory usage for a dataset.
(name: str, data: dict)
| 52 | |
| 53 | |
| 54 | def benchmark_memory(name: str, data: dict): |
| 55 | """Benchmark memory usage for a dataset.""" |
| 56 | print(f"\n{'='*60}") |
| 57 | print(f"Memory Benchmark: {name}") |
| 58 | print(f"{'='*60}") |
| 59 | |
| 60 | # Python object memory |
| 61 | obj_memory = get_memory_size(data) |
| 62 | print(f"\n📦 Python Object:") |
| 63 | print(f" Memory: {format_size(obj_memory):>10} ({obj_memory:,} bytes)") |
| 64 | |
| 65 | # JSON string memory |
| 66 | json_str = json.dumps(data, indent=2) |
| 67 | json_str_memory = sys.getsizeof(json_str) |
| 68 | json_bytes = json_str.encode('utf-8') |
| 69 | json_bytes_memory = len(json_bytes) |
| 70 | |
| 71 | print(f"\n📄 JSON Format:") |
| 72 | print(f" String object memory: {format_size(json_str_memory):>10} ({json_str_memory:,} bytes)") |
| 73 | print(f" UTF-8 bytes size: {format_size(json_bytes_memory):>10} ({json_bytes_memory:,} bytes)") |
| 74 | |
| 75 | # TOON string memory |
| 76 | toon_str = encode(data) |
| 77 | toon_str_memory = sys.getsizeof(toon_str) |
| 78 | toon_bytes = toon_str.encode('utf-8') |
| 79 | toon_bytes_memory = len(toon_bytes) |
| 80 | |
| 81 | print(f"\n🎯 TOON Format:") |
| 82 | print(f" String object memory: {format_size(toon_str_memory):>10} ({toon_str_memory:,} bytes)") |
| 83 | print(f" UTF-8 bytes size: {format_size(toon_bytes_memory):>10} ({toon_bytes_memory:,} bytes)") |
| 84 | |
| 85 | # Calculate savings |
| 86 | string_savings = ((json_str_memory - toon_str_memory) / json_str_memory) * 100 |
| 87 | bytes_savings = ((json_bytes_memory - toon_bytes_memory) / json_bytes_memory) * 100 |
| 88 | |
| 89 | print(f"\n💾 Memory Savings:") |
| 90 | print(f" String memory: {string_savings:.1f}% smaller") |
| 91 | print(f" Bytes size: {bytes_savings:.1f}% smaller") |
| 92 | |
| 93 | # Practical impact |
| 94 | print(f"\n💡 Practical Impact:") |
| 95 | print(f" If you send this data to an LLM API:") |
| 96 | print(f" • JSON uses {json_bytes_memory:,} bytes of network bandwidth") |
| 97 | print(f" • TOON uses {toon_bytes_memory:,} bytes of network bandwidth") |
| 98 | print(f" • You save {json_bytes_memory - toon_bytes_memory:,} bytes per request!") |
| 99 | |
| 100 | return { |
| 101 | 'name': name, |
| 102 | 'obj_memory': obj_memory, |
| 103 | 'json_str_memory': json_str_memory, |
| 104 | 'json_bytes_memory': json_bytes_memory, |
| 105 | 'toon_str_memory': toon_str_memory, |
| 106 | 'toon_bytes_memory': toon_bytes_memory, |
| 107 | 'string_savings': string_savings, |
| 108 | 'bytes_savings': bytes_savings, |
| 109 | } |
| 110 | |
| 111 |
no test coverage detected
searching dependent graphs…