Quickstart
In this guide, you will use Alnoms to detect a "Silent Trap"βa common \(O(N^2)\) bottleneck that looks harmless but fails at scale.
1. π Create a Target Script
Save the following code as slow_script.py. This function checks for duplicates using an inefficient nested lookup.
"""Inefficient script used for the Alnoms demonstration."""
def slow_membership_sum(arr):
total = 0
for x in arr:
# Intentional O(N^2) membership trap
if x in arr:
total += x
return total
# Required for empirical scaling
def data_gen(n):
return (list(range(n)),)
if __name__ == "__main__":
data = list(range(200))
print(slow_membership_sum(data))
2. β‘ Run the Audit
Execute a deep performance audit using the CLI:
3. π Read the Report
Alnoms will generate a comprehensive Performance Report. Review the following key diagnostic signals:
- π§ Detected Intent: The engine identifies the specific algorithmic pattern, such as a Membership test ('in arr') inside loop.
- π¨ Static Analysis: Alnoms pinpoints the exact file and line number, explaining why the pattern is inefficient (e.g., \(O(N)\) lookups inside a loop) and providing a Suggested Fix.
- π Empirical Scaling Analysis: The engine performs a doubling test to calculate the Scaling Ratio. A ratio near
4.0confirms an Est. Complexity of \(O(N^2)\). - βοΈ Verdict: A final performance grade is issued (e.g., WARNING: Function operates at O(N^2)), indicating if the code is safe for production scale.
- π Expected Impact: The report estimates the tangible performance gainβoften 100Γβ1000Γβachieved by shifting from quadratic to linear complexity.
- π After Optimization (Simulated): Alnoms provides the specific high-performance implementation, such as using
alnoms.dsa.structures.separate_chaining_hash_st, to achieve \(O(N)\) scaling.
==================================================
βοΈ PERFORMANCE REPORT
==================================================
File: examples/alnoms/scripts/slow_script.py
Timestamp (UTC): 2026-04-20T21:24:34.294600Z
Total Execution Time: 0.0035s
π§ DETECTED INTENT:
Membership test ('in arr') inside loop
π¨ STATIC ANALYSIS (Diagnostics & Suggestions)
--------------------------------------------------
1. β οΈ ISSUE: Membership test ('in arr') inside loop (Function: slow_membership_sum | Line: 7)
π Explanation: Membership checks on lists inside loops are O(N). Convert to a set for O(1) lookups.
π RECOMMENDED OPTIMIZATION: O(N)
ποΈ IMPLEMENTATION: alnoms.dsa.structures.separate_chaining_hash_st
π ACCESS TIER: OSS
β±οΈ Complexity Shift: O(N*M) β O(N + M)
π‘ SUGGESTED FIX:
--- BEFORE ---
| for k in keys:
| if k in items:
| process(k)
--- AFTER ---
| items_set = set(items)
| for k in keys:
| if k in items_set:
| process(k)
β±οΈ DYNAMIC PROFILING (Top Execution Bottlenecks)
--------------------------------------------------
1. slow_membership_sum() -> 7e-05s (2.15%)
π EMPIRICAL SCALING ANALYSIS: slow_membership_sum()
--------------------------------------------------
N | Time (s) | Ratio | Est. Complexity
--------------------------------------------------
50 | 0.00001 | - | Initial Round
100 | 0.00002 | 3.32 | O(N^2)
200 | 0.00008 | 3.91 | O(N^2)
400 | 0.00028 | 3.59 | O(N^2)
βοΈ VERDICT:
β οΈ WARNING: Function operates at O(N^2). May not scale efficiently.
π CONTEXT
--------------------------------------------------
Empirical scaling validates asymptotic behavior under increasing load.
π EXPECTED IMPACT
--------------------------------------------------
For N = 10,000:
β’ O(NΒ²) β ~100,000,000 operations
β’ O(N) β ~10,000 operations
Estimated improvement: 100Γβ1000Γ depending on workload.
π€ CONFIDENCE
--------------------------------------------------
High β static and empirical signals agree.
π AFTER OPTIMIZATION (SIMULATED)
--------------------------------------------------
Expected Complexity: O(N)
Behavior: Linear scaling with stable performance.
Suggested Implementation:
s = set(arr)
for x in arr:
if x in s:
total += x
==================================================
4. π§ Understanding the Ratios
The core of Alnomsβ intelligence lies in the Empirical Scaling Ratio. The engine calculates how much longer a function takes to execute when the input size \(N\) is doubled:
Visualizing the divergence of linear, quadratic, and cubic growth.
- Ratio \(2.0\): Linear scaling \(O(N)\). The execution time doubles with the data.
- Ratio \(4.0\): Quadratic scaling \(O(N^2)\). The execution time quadruples, indicating a nested loop or redundant scan.
- Ratio \(8.0\): Cubic scaling \(O(N^3)\). Serious structural bottleneck, often involving triple-nested logic.
5. π οΈ Apply the Fix
Alnoms doesn't just diagnose; it provides the Sovereign Implementation. In the example above, the engine recommended alnoms.dsa.structures.separate_chaining_hash_st.
To apply this professionally, refactor your code to use the Alnoms high-performance structure:
from alnoms.dsa.structures import SeparateChainingHashST
def fast_membership_sum(arr):
# Convert list to an optimized hash structure
st = SeparateChainingHashST()
for item in arr:
st.put(item, item)
total = 0
for x in arr:
# O(1) lookup vs original O(N)
if st.contains(x):
total += x
return total
# Required for empirical scaling
def data_gen(n):
return (list(range(n)),)
if __name__ == "__main__":
data = list(range(200))
print(fast_membership_sum(data))
6. π Re-run the Audit
Execute a deep performance audit using the CLI:
7. β Observe the Improvement
The report now validates that the scaling is linear.
==================================================
βοΈ PERFORMANCE REPORT
==================================================
File: examples/alnoms/scripts/fast_script.py
Timestamp (UTC): 2026-04-21T01:28:14.001488Z
Total Execution Time: 0.0106s
π§ DETECTED INTENT:
Potentially expensive call 'put()' inside loop
π¨ STATIC ANALYSIS (Diagnostics & Suggestions)
--------------------------------------------------
1. β οΈ ISSUE: Potentially expensive call 'put()' inside loop (Function: fast_membership_sum | Line: 8)
π Explanation: A costly function call is executed repeatedly. Cache/memoize the result or hoist it outside the loop.
π RECOMMENDED OPTIMIZATION: O(N)
ποΈ IMPLEMENTATION: functools.lru_cache
π ACCESS TIER: OSS
β±οΈ Complexity Shift: O(N * C) β O(N + unique(C))
π‘ SUGGESTED FIX:
--- BEFORE ---
| for item in items:
| value = expensive_fn(item)
| process(value)
--- AFTER ---
| cache = {}
| for item in items:
| if item not in cache:
| cache[item] = expensive_fn(item)
| process(cache[item])
β±οΈ DYNAMIC PROFILING (Top Execution Bottlenecks)
--------------------------------------------------
1. fast_membership_sum() -> 0.00041s (3.89%)
π EMPIRICAL SCALING ANALYSIS: fast_membership_sum()
--------------------------------------------------
N | Time (s) | Ratio | Est. Complexity
--------------------------------------------------
50 | 0.00006 | - | Initial Round
100 | 0.00009 | 1.43 | O(N)
200 | 0.00014 | 1.66 | O(N)
400 | 0.00026 | 1.83 | O(N)
βοΈ VERDICT:
β
PASSED: Function operates at O(N). Safe for scaling.
π CONTEXT
--------------------------------------------------
Empirical scaling validates asymptotic behavior under increasing load.
π EXPECTED IMPACT
--------------------------------------------------
For N = 10,000:
β’ O(NΒ²) β ~100,000,000 operations
β’ O(N) β ~10,000 operations
Estimated improvement: 100Γβ1000Γ depending on workload.
π€ CONFIDENCE
--------------------------------------------------
Medium β mixed signals between analysis methods.
π AFTER OPTIMIZATION (SIMULATED)
--------------------------------------------------
Expected Complexity: O(N)
Behavior: Linear scaling with stable performance.
Suggested Implementation:
s = set(arr)
for x in arr:
if x in s:
total += x
==================================================
βΆοΈ Next Steps
Now that you have executed your first audit:
- 𧬠Explore the Pattern Registry: See the full list of anti-patterns Alnoms can detect.
- ποΈ Configure the Core Engine: Learn how to customize the Profiler for your specific hardware.
- π Review Case Studies: See how other systems have been optimized using Alnoms.