Python vs C++: Analyzing the Efficiency of Longest Substring Algorithms
This article analyzes the efficiency of two different implementations for the 'Longest Substring Without Repeating Characters' problem, one written in C++ (Code 1) and the other in Python (Code 2). The focus is on understanding why the Python code demonstrates a faster execution time compared to the C++ version.
Code 1 (C++):
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> occ;
int n = s.size(),rk = -1, ans = 0;
for (int i = 0; i < n; i++) {
if (i) occ.erase(s[i - 1]);
while (rk + 1 < n && !occ.count(s[rk + 1])) {
occ.emplace(s[rk + 1]);
rk++;
}
ans = max(ans, rk - i + 1);
}
return ans;
}
};
Code 2 (Python):
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
maxLen = 0
beginning = 0
map = {}
chars = list(s)
for terminal, c in enumerate(chars):
if c in map:
beginning = max(beginning, map[c])
map[c] = terminal + 1
maxLen = max(maxLen, terminal - beginning + 1)
return maxLen
Performance Analysis
The key difference in performance lies in the data structures used and their respective time complexities. Code 1 utilizes an unordered_set in C++ to track the occurrence of characters. The unordered_set offers an average time complexity of O(1) for insertion, deletion, and lookup. However, in the worst case scenario, these operations can degrade to O(n), where n is the number of elements in the set.
In contrast, Code 2 employs a dictionary (hash table) in Python. Dictionaries provide constant time (O(1)) for key-value lookups, insertions, and deletions on average. This constant-time access is a significant advantage over the potential O(n) complexity of the unordered_set.
Furthermore, Python's dictionary, in this case, leverages hashing for efficient key-value mapping. This hash-based approach makes the dictionary highly optimized for retrieving the last seen position of a character within the string, which is crucial for determining the length of the longest non-repeating substring.
Another factor contributing to the speed difference is the use of an array (list) in Python to store characters from the string. Arrays in Python allow for fast access to elements with O(1) time complexity, whereas C++'s string access using indexing can incur a slight performance overhead in certain cases.
Conclusion
The Python code's use of dictionaries and arrays, both of which provide near-constant time operations, makes it significantly faster than the C++ code's reliance on an unordered_set, which can exhibit variable time complexity. In scenarios where efficiency is paramount, leveraging data structures that guarantee consistent performance is crucial. While C++ offers more direct control and potential for optimization, Python's built-in data structures, such as dictionaries, often provide a significant performance edge, especially for applications requiring frequent lookups and insertions.
原文地址: https://www.cveoy.top/t/topic/p9WA 著作权归作者所有。请勿转载和采集!