Skip to content

Instantly share code, notes, and snippets.

@Passific
Created March 24, 2026 09:43
Show Gist options
  • Select an option

  • Save Passific/a20289fedca04baa64fe4129eb1317e5 to your computer and use it in GitHub Desktop.

Select an option

Save Passific/a20289fedca04baa64fe4129eb1317e5 to your computer and use it in GitHub Desktop.
# For _budget.id=3 (second item)
python3 ./test.py
0.3989869259967236
0.6314781839973875
# For _budget.id=8 (last item)
python3 ./test.py
0.5605128600000171
0.6435106829958386
import timeit
def test1():
_budget = {"id": 3, "value": 6}
data_budgets = [{"id": 5, "value": 4}, {"id": 3, "value": 2}, {"id": 15, "value": 4}, {"id": 25, "value": 4}, {"id": 35, "value": 4}, {"id": 45, "value": 4}, {"id": 55, "value": 4}, {"id": 65, "value": 4}, {"id": 8, "value": 4}]
for budget in data_budgets:
if budget["id"] == _budget["id"]:
_budget = budget
break
def test2():
_budget = {"id": 3, "value": 6}
data_budgets = [{"id": 5, "value": 4}, {"id": 3, "value": 2}, {"id": 15, "value": 4}, {"id": 25, "value": 4}, {"id": 35, "value": 4}, {"id": 45, "value": 4}, {"id": 55, "value": 4}, {"id": 65, "value": 4}, {"id": 8, "value": 4}]
budget = {b["id"]: b for b in data_budgets}
_budget = budget.get(_budget["id"], _budget)
print(timeit.timeit("test1()", globals=locals()))
print(timeit.timeit("test2()", globals=locals()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment