Created
November 13, 2024 20:07
-
-
Save samukasmk/c26926887c3597b4884bdd90a1ce91ef to your computer and use it in GitHub Desktop.
Comparing set() operations in python language and showing differences in operations using [.difference() vs -] and [.intersection() vs &]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
# | |
# Creating two large sets for testing | |
# | |
a = set(range(1, 1000000)) # Set with 1 million elements | |
b = set(range(500000, 1500000)) # Set that partially overlaps with set 'a' | |
print() | |
# | |
# Comparing: .difference vs - | |
# | |
# Measuring time with the '-' operator | |
start = time.perf_counter() | |
difference_operator = a - b | |
end = time.perf_counter() | |
operator_time = end - start | |
print(f"[Comparing: .difference vs -] Time with '-' operator: {operator_time:.6f} seconds") | |
# Measuring time with the .difference() method | |
start = time.perf_counter() | |
difference_method = a.difference(b) | |
end = time.perf_counter() | |
method_time = end - start | |
print(f"[Comparing: .difference vs -] Time with .difference() method: {method_time:.6f} seconds") | |
# Comparing execution times | |
print(f"[Comparing: .difference vs -] '-' operator is faster by {method_time - operator_time:.6f} seconds") | |
# Comparing results | |
if difference_operator == difference_method: | |
print(f"[Comparing: .difference vs -] The results of both operations are identical, with {len(difference_method)} elements in the set") | |
else: | |
print(f"[Comparing: .difference vs -] The results of the two operations are different!!!") | |
# | |
# Comparing: .intersection vs & | |
# | |
# Measuring time with the '&' operator | |
start = time.perf_counter() | |
intersection_operator = a & b | |
end = time.perf_counter() | |
operator_time = end - start | |
print(f"[Comparing: .intersection vs &] Time with '&' operator: {operator_time:.6f} seconds") | |
# Measuring time with the .intersection() method | |
start = time.perf_counter() | |
intersection_method = a.intersection(b) | |
end = time.perf_counter() | |
method_time = end - start | |
print(f"[Comparing: .intersection vs &] Time with .intersection() method: {method_time:.6f} seconds") | |
# Comparing execution times | |
print(f"[Comparing: .intersection vs &] '&' operator is faster by {method_time - operator_time:.6f} seconds") | |
# Comparing results | |
if intersection_operator == intersection_method: | |
print(f"[Comparing: .intersection vs &] The results of both operations are identical, with {len(intersection_method)} elements in the set") | |
else: | |
print(f"[Comparing: .intersection vs &] The results of the two operations are different!!!") | |
# | |
# Running on my machine | |
# > Proc: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz | |
# > Ram: 32GB | |
# > Python: 3.12.2 | |
# | |
""" | |
$ python comparing_set_operations.py | |
[Comparing: .difference vs -] Time with '-' operator: 0.021657 seconds | |
[Comparing: .difference vs -] Time with .difference() method: 0.026557 seconds | |
[Comparing: .difference vs -] '-' operator is faster by 0.004900 seconds | |
[Comparing: .difference vs -] The results of both operations are identical, with 499999 elements in the set | |
[Comparing: .intersection vs &] Time with '&' operator: 0.026268 seconds | |
[Comparing: .intersection vs &] Time with .intersection() method: 0.026485 seconds | |
[Comparing: .intersection vs &] '&' operator is faster by 0.000218 seconds | |
[Comparing: .intersection vs &] The results of both operations are identical, with 500000 elements in the set | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment