configparser vs. pyyaml: ini vs. yaml (Benchmarking)
When managing configuration files in Python, we have two popular choices: configparser
for INI files and PyYAML
for YAML files.
This tutorial compares configparser
and PyYAML
by benchmarking their parsing speed, memory usage, writing performance, and modification speed.
Parsing Benchmark Test
You can use the timeit
module to benchmark parsing speed between configparser
and PyYAML
.
import configparser import yaml import timeit ini_data = """ [Database] host=localhost port=3306 user=Ahmad password=secret """ yaml_data = """ Database: host: localhost port: 3306 user: Ahmad password: secret """ def parse_ini(): config = configparser.ConfigParser() config.read_string(ini_data) def parse_yaml(): config = yaml.safe_load(yaml_data) ini_time = timeit.timeit(parse_ini, number=10000) yaml_time = timeit.timeit(parse_yaml, number=10000) print(f"INI parsing time: {ini_time}") print(f"YAML parsing time: {yaml_time}")
Output:
INI parsing time: 1.3473019000011845 YAML parsing time: 6.746310700000322
INI files are parsed much faster than YAML files in this benchmark which makes configparser
more efficient.
Memory Usage
Measuring memory consumption helps determine which library is more memory-efficient during parsing.
import configparser import yaml import tracemalloc ini_data = """ [Database] host=localhost port=3306 user=Ahmad password=secret """ yaml_data = """ Database: host: localhost port: 3306 user: Ahmad password: secret """ tracemalloc.start() config = configparser.ConfigParser() config.read_string(ini_data) ini_memory = tracemalloc.get_traced_memory() tracemalloc.stop() tracemalloc.start() config = yaml.safe_load(yaml_data) yaml_memory = tracemalloc.get_traced_memory() tracemalloc.stop() print(f"INI memory usage: {ini_memory[1] - ini_memory[0]} bytes") print(f"YAML memory usage: {yaml_memory[1] - yaml_memory[0]} bytes")
Output:
INI memory usage: 2350 bytes YAML memory usage: 10810 bytes
configparser
consumes less memory compared to PyYAML
.
Writing Performance Test
Evaluate how quickly each library can write configuration data to a file.
import configparser import yaml import timeit config_data = { 'Database': { 'host': 'localhost', 'port': 3306, 'user': 'Sara', 'password': 'secret' } } def write_ini(): config = configparser.ConfigParser() config.read_dict(config_data) with open('config.ini', 'w') as f: config.write(f) def write_yaml(): with open('config.yaml', 'w') as f: yaml.dump(config_data, f) ini_time = timeit.timeit(write_ini, number=1000) yaml_time = timeit.timeit(write_yaml, number=1000) print(f"INI writing time: {ini_time}") print(f"YAML writing time: {yaml_time}")
Output:
INI writing time: 0.4919393999989552 YAML writing time: 0.6114596000006713
Writing INI files with configparser
is a bit faster than writing YAML files with PyYAML
.
Modification Speed
You can measure how quickly each library handles modifications to existing configuration data by changing data in both files and measuring the time.
import configparser import yaml import timeit ini_data = """ [Database] host=localhost port=3306 user=Ahmad password=secret """ yaml_data = """ Database: host: localhost port: 3306 user: Ahmad password: secret """ def modify_ini(): config = configparser.ConfigParser() config.read_string(ini_data) config.set('Database', 'port', '3307') def modify_yaml(): config = yaml.safe_load(yaml_data) config['Database']['port'] = 3307 ini_time = timeit.timeit(modify_ini, number=10000) yaml_time = timeit.timeit(modify_yaml, number=10000) print(f"INI modification time: {ini_time}") print(f"YAML modification time: {yaml_time}")
Output:
INI modification time: 0.7588212999980897 YAML modification time: 3.409955799997988
Modifying YAML data with PyYAML
is much faster than using configparser
for INI files.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.