Port scanning with Nmap in Cybersecurity - Time & Space Complexity
When using Nmap to scan ports, it's important to understand how the time it takes grows as the number of ports increases.
We want to know how the scanning effort changes when scanning more ports.
Analyze the time complexity of the following Nmap port scanning command.
nmap -p 1-1000 192.168.1.1
This command scans ports 1 through 1000 on the target IP address to check which ports are open.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending a probe to each port to check its status.
- How many times: Once for each port in the specified range (e.g., 1000 times for ports 1 to 1000).
As the number of ports to scan increases, the number of probes sent grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 probes sent |
| 100 | 100 probes sent |
| 1000 | 1000 probes sent |
Pattern observation: The number of operations grows directly with the number of ports scanned.
Time Complexity: O(n)
This means the scanning time increases in a straight line as you scan more ports.
[X] Wrong: "Scanning more ports takes the same time because the tool is fast."
[OK] Correct: Each port requires a separate check, so more ports mean more work and more time.
Understanding how scanning time grows helps you explain network scanning efficiency and resource use clearly in real situations.
"What if Nmap scanned ports in parallel instead of one by one? How would the time complexity change?"