Skip to content

Commit

Permalink
Benchmark Edge Id
Browse files Browse the repository at this point in the history
  • Loading branch information
bcollazo committed Apr 2, 2023
1 parent a7f15b3 commit d541af9
Showing 1 changed file with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import timeit


result = timeit.timeit("tuple(sorted((a,b)))", setup="a = 20; b = 45", number=2_000_000)
print("TODAY", result)

result = timeit.timeit(
"(a,b) if a < b else (b,a)", setup="a = 20; b = 45", number=2_000_000
)
print("TERNARY", result)

result = timeit.timeit(
"edge_id((a, b))",
setup="a = 20; b = 45; edge_id = lambda x: (x[0],x[1]) if x[0] < x[1] else (x[1],x[0])",
number=2_000_000,
)
print("FUNC TERNARY", result)

result = timeit.timeit(
"edge_id(a, b)",
setup="a = 20; b = 45; edge_id = lambda x, y: (x,y) if x < y else (y,x)",
number=2_000_000,
)
print("FUNC TERNARY 2 PARAMS", result)

result = timeit.timeit(
"(min(a, b), max(a, b))", setup="a = 20; b = 45", number=2_000_000
)
print("MIN MAX", result)

# I like map lookup because makes it easy to change to INT ids in the future (a more
# compact representation of an edge)
result = timeit.timeit(
"edge_id[(a,b)]", setup="a = 20; b = 45; edge_id = {(a,b): 1}", number=2_000_000
)
print("MAP LOOKUP", result)

0 comments on commit d541af9

Please sign in to comment.