Optimizing
The Need for Speed
Baseline Performance (Linear Search)
Requests
4,128
Time
1,005.67 seconds (~17 min)
We can drastically improve this with better algorithms.
Algorithm 1: Bisection (Binary Search)
How It Works
Repeatedly split the search area in half until one option remains.
Search area: ASCII values 0-127
Example: Finding '-' (ASCII 45)
Target = '-' = 45
LBound = 0, UBound = 127
β Midpoint = (0+127)//2 = 63
β Is target between 0 and 63? YES
β UBound = 63 - 1 = 62
LBound = 0, UBound = 62
β Midpoint = (0+62)//2 = 31
β Is target between 0 and 31? NO
β LBound = 31 + 1 = 32
LBound = 32, UBound = 62
β Midpoint = (32+62)//2 = 47
β Is target between 32 and 47? YES
β UBound = 47 - 1 = 46
LBound = 32, UBound = 46
β Midpoint = (32+46)//2 = 39
β Is target between 32 and 39? NO
β LBound = 39 + 1 = 40
LBound = 40, UBound = 46
β Midpoint = (40+46)//2 = 43
β Is target between 40 and 43? NO
β LBound = 43 + 1 = 44
LBound = 44, UBound = 46
β Midpoint = (44+46)//2 = 45
β Is target between 44 and 45? YES
β UBound = 45 - 1 = 44
LBound = 44, UBound = 44
β Midpoint = (44+44)//2 = 44
β Is target between 44 and 44? NO
β LBound = 44 + 1 = 45
β
LBound = 45 = Target!Result: 7 requests instead of 45!
SQL Query
Python Implementation
Performance
Requests
256
Time
61.56 seconds
Improvement
16x faster!
Algorithm 2: SQL-Anding (Bitwise)
How It Works
ASCII values 0-127 = binary 00000000 to 01111111
Since MSB is always 0, we only need to dump 7 bits.
Bitwise AND Logic
Example: Finding '9' (ASCII 57)
SQL Query
Where X = 1, 2, 4, 8, 16, 32, 64 (powers of 2)
Python Implementation
Performance
Requests
256
Time
60.28 seconds
Note
Slightly faster due to simpler query
Performance Comparison
Linear Search
4,128
1,005.67s
Baseline
Bisection
256
61.56s
16x faster
SQL-Anding
256
60.28s
16.7x faster
Further Optimization: Multithreading
Bisection Threading
7 requests per character are dependent (sequential)
Individual characters are independent (parallel)
SQL-Anding Threading
7 requests per character are independent (parallel)
Individual characters are independent (parallel)
All requests can run in parallel!
Algorithm Selection Guide
Single-threaded
SQL-Anding (slightly faster)
Multi-threaded
SQL-Anding (fully parallelizable)
Limited requests
Both equal (same request count)
Simple implementation
Bisection (easier to understand)
Quick Reference
Bisection Query
SQL-Anding Query
Complexity
Linear
~64 average (0-127)
Bisection
7 (logβ 128)
SQL-Anding
7 (7 bits)
References
Last updated