Today I discovered practical optimizations in Python’s collections module and explored Linux display management tools that make working with multiple monitors much easier in tiling window managers.
Python Collections Performance
defaultdict vs dict.get() Performance
collections.defaultdict
provides better performance than checking {}.get()
for handling missing keys:
|
|
Advanced defaultdict Usage Patterns
|
|
Why defaultdict is faster than dict.get():
- Single hash lookup - defaultdict only hashes the key once
- No function call overhead - dict.get() requires a method call
- Optimized C implementation - Less Python bytecode execution
- No conditional logic - Automatic default value creation
- Memory efficiency - Reduces object creation overhead
Python’s Sorted Function with Reverse
The sorted
function has a convenient reverse
flag for descending order:
|
|
When to use sorted() vs list.sort():
- sorted(): When you need to keep the original list unchanged
- list.sort(): When you want to modify the list in-place (more memory efficient)
- reverse=True: Always prefer this over manual reversal
- Complex keys: Use lambda functions or operator.itemgetter for clarity
- Multiple criteria: Consider using tuple keys with negation for mixed sort orders
Linux Display Management
arandr: GUI Frontend for xrandr
arandr
provides a visual interface for configuring multiple monitors in tiling window managers:
|
|
xrandr Command Line Usage
|
|
Integration with Tiling Window Managers
|
|
|
|
Automated Monitor Detection
|
|
Typical Multi-Monitor Setup Process:
- Connect monitors and boot system
- Launch arandr to visually arrange displays
- Save configuration as shell script
- Integrate with window manager for automatic application
- Create hotkeys for switching between configurations
- Set up hotplug detection for dynamic configuration
Advanced xrandr Techniques
|
|
Key Insights
Python Performance Optimization
Today’s exploration of collections.defaultdict
demonstrates how choosing the right data structure can provide measurable performance improvements:
- Algorithmic efficiency - Single hash lookup vs multiple operations
- Implementation details - C-level optimizations in standard library
- Memory patterns - Reduced object creation and method call overhead
- Code clarity - Simpler, more readable code often performs better
Linux Desktop Productivity
The combination of arandr
and xrandr
showcases how GUI tools can complement command-line utilities:
- Visual configuration - arandr for initial setup and experimentation
- Scriptable automation - xrandr for reproducible configurations
- Integration points - Window manager hooks and hotplug detection
- Workflow optimization - Saved configurations for different scenarios
Development Environment Considerations
Both topics relate to optimizing development environments:
- Code performance affects development feedback loops
- Monitor configuration impacts workspace efficiency and productivity
- Automation reduces manual configuration overhead
- Tool integration creates seamless workflows
Today’s learning reinforced that small optimizations in both code and environment setup can compound to create significant productivity improvements over time.