|
2 | 2 |
|
3 | 3 | The `bytes` type is an immutable sequence of bytes, while `bytearray` is the mutable equivalent. |
4 | 4 |
|
5 | | -## Time Complexity |
| 5 | +## Complexity Reference |
6 | 6 |
|
7 | 7 | ### Bytes |
8 | 8 |
|
9 | | -| Operation | Time | Notes | |
10 | | -|-----------|------|-------| |
11 | | -| `len()` | O(1) | Direct lookup | |
12 | | -| `access[i]` | O(1) | Direct indexing | |
13 | | -| `in` (membership) | O(n) | Linear search | |
14 | | -| **Search** ||| |
15 | | -| `find(sub)` | O(n + m) worst for long strings, O(n*m) worst for pathological cases | n = bytes length, m = pattern length | |
16 | | -| `rfind(sub)` | O(n*m) worst case | Search from right | |
17 | | -| `index(sub)` | O(n) | Like find() but raises ValueError | |
18 | | -| `rindex(sub)` | O(n*m) worst case | Like rfind() but raises ValueError | |
19 | | -| `count(sub)` | O(n) | Count non-overlapping occurrences | |
20 | | -| `startswith(prefix)` | O(m) | m = prefix length | |
21 | | -| `endswith(suffix)` | O(m) | m = suffix length | |
22 | | -| **Replace/Translate** ||| |
23 | | -| `replace(old, new)` | O(n) | Creates new bytes object | |
24 | | -| `translate(table)` | O(n) | Single pass with table lookup | |
25 | | -| `maketrans(from, to)` | O(k) | k = mapping size; static method | |
26 | | -| `expandtabs(tabsize)` | O(n) | Replace tabs with spaces | |
27 | | -| `removeprefix(prefix)` | O(n) | Returns slice if prefix matches (Python 3.9+) | |
28 | | -| `removesuffix(suffix)` | O(n) | Returns slice if suffix matches (Python 3.9+) | |
29 | | -| **Split/Join** ||| |
30 | | -| `split(sep)` | O(n) | Single pass | |
31 | | -| `rsplit(sep)` | O(n) | Split from right | |
32 | | -| `splitlines()` | O(n) | Split on line boundaries | |
33 | | -| `partition(sep)` | O(n) | Split into 3-tuple at first sep | |
34 | | -| `rpartition(sep)` | O(n) | Split into 3-tuple at last sep | |
35 | | -| `join(iterable)` | O(n) | n = total output length | |
36 | | -| **Case Conversion** ||| |
37 | | -| `upper()` | O(n) | ASCII uppercase | |
38 | | -| `lower()` | O(n) | ASCII lowercase | |
39 | | -| `capitalize()` | O(n) | Uppercase first, lowercase rest | |
40 | | -| `title()` | O(n) | Titlecase words | |
41 | | -| `swapcase()` | O(n) | Swap upper/lower | |
42 | | -| **Stripping** ||| |
43 | | -| `strip(chars)` | O(n) | Remove from both ends | |
44 | | -| `lstrip(chars)` | O(n) | Remove from left | |
45 | | -| `rstrip(chars)` | O(n) | Remove from right | |
46 | | -| **Padding/Alignment** ||| |
47 | | -| `center(width)` | O(n) | Pad both sides | |
48 | | -| `ljust(width)` | O(n) | Pad right side | |
49 | | -| `rjust(width)` | O(n) | Pad left side | |
50 | | -| `zfill(width)` | O(n) | Pad with zeros | |
51 | | -| **Predicates** ||| |
52 | | -| `isalnum()` | O(n) | Check alphanumeric | |
53 | | -| `isalpha()` | O(n) | Check alphabetic | |
54 | | -| `isascii()` | O(n) | Check ASCII (Python 3.7+) | |
55 | | -| `isdigit()` | O(n) | Check digit chars | |
56 | | -| `islower()` | O(n) | Check lowercase | |
57 | | -| `isspace()` | O(n) | Check whitespace | |
58 | | -| `istitle()` | O(n) | Check titlecase | |
59 | | -| `isupper()` | O(n) | Check uppercase | |
60 | | -| **Encoding** ||| |
61 | | -| `decode(encoding)` | O(n) | Convert to string | |
62 | | -| `hex()` | O(n) | Convert to hex string | |
63 | | -| `fromhex(string)` | O(n) | Create bytes from hex; class method | |
| 9 | +| Operation | Time | Space | Notes | |
| 10 | +|-----------|------|-------|-------| |
| 11 | +| `len()` | O(1) | O(1) | Direct lookup | |
| 12 | +| `access[i]` | O(1) | O(1) | Direct indexing | |
| 13 | +| `in` (membership) | O(n) | O(1) | Linear search | |
| 14 | +| **Search** |||| |
| 15 | +| `find(sub)` | O(n + m) avg | O(1) | n = bytes length, m = pattern length | |
| 16 | +| `rfind(sub)` | O(n*m) worst | O(1) | Search from right | |
| 17 | +| `index(sub)` | O(n) | O(1) | Like find() but raises ValueError | |
| 18 | +| `rindex(sub)` | O(n*m) worst | O(1) | Like rfind() but raises ValueError | |
| 19 | +| `count(sub)` | O(n) | O(1) | Count non-overlapping occurrences | |
| 20 | +| `startswith(prefix)` | O(m) | O(1) | m = prefix length | |
| 21 | +| `endswith(suffix)` | O(m) | O(1) | m = suffix length | |
| 22 | +| **Replace/Translate** |||| |
| 23 | +| `replace(old, new)` | O(n) | O(n) | Creates new bytes object | |
| 24 | +| `translate(table)` | O(n) | O(n) | Single pass with table lookup | |
| 25 | +| `maketrans(from, to)` | O(k) | O(k) | k = mapping size; static method | |
| 26 | +| `expandtabs(tabsize)` | O(n) | O(n) | Replace tabs with spaces | |
| 27 | +| `removeprefix(prefix)` | O(n) | O(n) | Returns slice if prefix matches (Python 3.9+) | |
| 28 | +| `removesuffix(suffix)` | O(n) | O(n) | Returns slice if suffix matches (Python 3.9+) | |
| 29 | +| **Split/Join** |||| |
| 30 | +| `split(sep)` | O(n) | O(n) | Single pass | |
| 31 | +| `rsplit(sep)` | O(n) | O(n) | Split from right | |
| 32 | +| `splitlines()` | O(n) | O(n) | Split on line boundaries | |
| 33 | +| `partition(sep)` | O(n) | O(n) | Split into 3-tuple at first sep | |
| 34 | +| `rpartition(sep)` | O(n) | O(n) | Split into 3-tuple at last sep | |
| 35 | +| `join(iterable)` | O(n) | O(n) | n = total output length | |
| 36 | +| **Case Conversion** |||| |
| 37 | +| `upper()` | O(n) | O(n) | ASCII uppercase | |
| 38 | +| `lower()` | O(n) | O(n) | ASCII lowercase | |
| 39 | +| `capitalize()` | O(n) | O(n) | Uppercase first, lowercase rest | |
| 40 | +| `title()` | O(n) | O(n) | Titlecase words | |
| 41 | +| `swapcase()` | O(n) | O(n) | Swap upper/lower | |
| 42 | +| **Stripping** |||| |
| 43 | +| `strip(chars)` | O(n) | O(n) | Remove from both ends | |
| 44 | +| `lstrip(chars)` | O(n) | O(n) | Remove from left | |
| 45 | +| `rstrip(chars)` | O(n) | O(n) | Remove from right | |
| 46 | +| **Padding/Alignment** |||| |
| 47 | +| `center(width)` | O(n) | O(n) | Pad both sides | |
| 48 | +| `ljust(width)` | O(n) | O(n) | Pad right side | |
| 49 | +| `rjust(width)` | O(n) | O(n) | Pad left side | |
| 50 | +| `zfill(width)` | O(n) | O(n) | Pad with zeros | |
| 51 | +| **Predicates** |||| |
| 52 | +| `isalnum()` | O(n) | O(1) | Check alphanumeric | |
| 53 | +| `isalpha()` | O(n) | O(1) | Check alphabetic | |
| 54 | +| `isascii()` | O(n) | O(1) | Check ASCII (Python 3.7+) | |
| 55 | +| `isdigit()` | O(n) | O(1) | Check digit chars | |
| 56 | +| `islower()` | O(n) | O(1) | Check lowercase | |
| 57 | +| `isspace()` | O(n) | O(1) | Check whitespace | |
| 58 | +| `istitle()` | O(n) | O(1) | Check titlecase | |
| 59 | +| `isupper()` | O(n) | O(1) | Check uppercase | |
| 60 | +| **Encoding** |||| |
| 61 | +| `decode(encoding)` | O(n) | O(n) | Convert to string | |
| 62 | +| `hex()` | O(n) | O(n) | Convert to hex string | |
| 63 | +| `fromhex(string)` | O(n) | O(n) | Create bytes from hex; class method | |
64 | 64 |
|
65 | 65 | *Note: bytes is immutable, so operations that appear to modify return new objects (O(n) space).* |
66 | 66 |
|
67 | 67 | ### Bytearray |
68 | 68 |
|
69 | | -| Operation | Time | Notes | |
70 | | -|-----------|------|-------| |
71 | | -| `append(x)` | O(1)* | Amortized, may resize | |
72 | | -| `insert(i, x)` | O(n) | Shift elements | |
73 | | -| `extend(iterable)` | O(k) | k = length | |
74 | | -| `pop()` | O(1) | Remove last | |
75 | | -| `pop(0)` | O(n) | Shift remaining | |
76 | | -| `remove(x)` | O(n) | Search and remove | |
77 | | -| `clear()` | O(n) | Deallocate | |
78 | | -| `copy()` | O(n) | Shallow copy | |
79 | | -| `reverse()` | O(n) | Reverse in-place | |
80 | | -| `resize(n)` | O(n) | Resize to n bytes; may truncate or zero-fill | |
81 | | - |
82 | | -## Space Complexity |
83 | | - |
84 | | -| Operation | Space | |
85 | | -|-----------|-------| |
86 | | -| `bytes()` copy | O(n) | |
87 | | -| `bytearray()` operations | O(1) amortized | |
88 | | -| `decode()` | O(n) for string | |
89 | | -| `join()` | O(n) result | |
| 69 | +| Operation | Time | Space | Notes | |
| 70 | +|-----------|------|-------|-------| |
| 71 | +| `append(x)` | O(1)* | O(1) | Amortized, may resize | |
| 72 | +| `insert(i, x)` | O(n) | O(1) | Shift elements | |
| 73 | +| `extend(iterable)` | O(k) | O(k) | k = length | |
| 74 | +| `pop()` | O(1) | O(1) | Remove last | |
| 75 | +| `pop(0)` | O(n) | O(1) | Shift remaining | |
| 76 | +| `remove(x)` | O(n) | O(1) | Search and remove | |
| 77 | +| `clear()` | O(n) | O(1) | Deallocate | |
| 78 | +| `copy()` | O(n) | O(n) | Shallow copy | |
| 79 | +| `reverse()` | O(n) | O(1) | Reverse in-place | |
| 80 | +| `resize(n)` | O(n) | O(1) | Resize to n bytes; may truncate or zero-fill | |
90 | 81 |
|
91 | 82 | ## Implementation Details |
92 | 83 |
|
|
0 commit comments