IRB customization in Ruby - Time & Space Complexity
When customizing IRB, we often run code that sets up the environment. Understanding how the time to apply these customizations grows helps us keep the setup fast.
We want to know how the time to load and apply custom settings changes as the customization code grows.
Analyze the time complexity of this IRB customization snippet.
require 'irb'
IRB.conf[:PROMPT][:MY_PROMPT] = {
PROMPT_I: '>> ',
PROMPT_S: '>> ',
PROMPT_C: '?> ',
PROMPT_N: '>> ',
RETURN: '=> %s\n'
}
IRB.conf[:PROMPT_MODE] = :MY_PROMPT
IRB.start
This code sets a custom prompt style for IRB and then starts the IRB session with that style.
Look for repeated actions that affect time.
- Primary operation: Assigning prompt settings in a hash and starting IRB.
- How many times: Each assignment happens once; IRB.start runs the interactive session once.
As the number of customization lines grows, the time to apply them grows roughly in a straight line.
| Input Size (lines of customization) | Approx. Operations |
|---|---|
| 10 | 10 assignments + 1 IRB start |
| 100 | 100 assignments + 1 IRB start |
| 1000 | 1000 assignments + 1 IRB start |
Pattern observation: The time grows linearly with the number of customization lines.
Time Complexity: O(n)
This means the time to apply customizations grows directly with how many lines of setup code you have.
[X] Wrong: "Adding more customization lines won't affect startup time much because it's just configuration."
[OK] Correct: Each line runs code that takes time, so more lines mean more time to process before IRB starts.
Knowing how setup time grows helps you write efficient startup scripts and shows you understand how code size affects performance.
"What if we changed the customization to load settings from a file instead of inline code? How would the time complexity change?"