HarshCasper / Rotten-Scripts

Scripts that will make you go WOW!
MIT License
1.47k stars 491 forks source link

Using literal syntax to create the data structure #1281

Closed Spratiher9 closed 2 years ago

Spratiher9 commented 2 years ago

Using the literal syntax can give minor performance bumps compared to using function calls to create dict, list and tuple.


In [1]: timeit.timeit(stmt="dict()", number=100000000)
Out[1]: 9.560388602000103

In [2]: timeit.timeit(stmt="{}", number=100000000)
Out[2]: 1.685333584000091

In [3]: timeit.timeit(stmt="tuple()", number=100000000)
Out[3]: 4.509182139000131

In [4]: timeit.timeit(stmt="()", number=100000000)
Out[4]: 0.5455615430000762

In [5]: timeit.timeit(stmt="list()", number=100000000)
Out[5]: 7.356000728000254

In [6]: timeit.timeit(stmt="[]", number=100000000)
Out[6]: 1.573771306999788

This is because here, the name dict must be looked up in the global scope in case it has been rebound. Same goes for the other two types list() and tuple().

ghost commented 2 years ago

Changes to your CodeSee Architecture Map:

View a CodeSee Map of these changes

Review in an interactive map

View more CodeSee Maps

Legend

CodeSee Map Legend

github-actions[bot] commented 2 years ago

@Spratiher9 Hurray, your PR got merged 🥳. Thanks a bunch. Feel free to take another Issue.💖