Skip to main content
1 of 2

Why initialize Set from dict.keys() is faster than using set.add()?

Code

total_exc_time = 0 for _ in range(10): start = time.time() set_a = set() dict_a = dict() add = set_a.add for index in range(1000000): dict_a[index] = index add(index) total_exc_time += ((time.time() - start) * 1000) total_exc_time = 0 for _ in range(10): `start = time.time()` `dict_a = dict()` for index in range(1000000): dict_a[index] = index set_a = set(dict_a.keys()) total_exc_time += ((time.time() - start) * 1000) 

Result

332.3066234588623 ms 283.2982540130615 ms 

isn't the time complexity for the first code is O(n) and later code is 2 times O(n)?