Created
February 23, 2020 17:47
-
-
Save EkremDincel/a3a7fa70fd2a1b07730f0c52146e5d05 to your computer and use it in GitHub Desktop.
Python comparison of set and list performance
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
from timeit import timeit as _timeit | |
uzunluk = 10000 | |
orta = int(uzunluk / 2) | |
s_number = uzunluk | |
l_number = uzunluk | |
timeit = lambda code, number = 1000: _timeit(code, globals = globals(), number = number) | |
s = {i for i in range(uzunluk)} | |
l = [i for i in range(uzunluk)] | |
def set_contain(): | |
orta in s | |
def set_iterate(): | |
for i in s: | |
pass | |
def set_add(): | |
global s_number | |
s_number += 1 | |
s.add(s_number) | |
def set_remove(): | |
global s_number | |
s.remove(s_number) | |
s_number -= 1 | |
def set_pop(): | |
s.pop() | |
######## | |
def list_contain(): | |
orta in l | |
def list_iterate(): | |
for i in l: | |
pass | |
def list_add(): | |
global l_number | |
l_number += 1 | |
l.append(l_number) | |
def list_remove(): | |
global l_number | |
l.remove(l_number) | |
l_number -= 1 | |
def list_pop(): | |
l.pop(orta) | |
def debug(başlık, liste, küme): | |
liste = timeit(liste) | |
küme = timeit(küme) | |
print(başlık, "\n", "list/set =", liste / küme, "\n\n","List:", liste, "Set:", küme, "\n\n", "-"*30, "\n") | |
def converter(a, b): | |
type_a = type(a) | |
type_b = type(b) | |
def convert(): | |
type_a(b) | |
return convert | |
debug("Contain", list_contain, set_contain) | |
debug("İterate", list_iterate, set_iterate) | |
debug("Add", list_add, set_add) | |
debug("Remove", list_remove, set_remove) | |
debug("Pop", list_pop, set_pop) | |
print("Convert list to set:", timeit(converter(s ,l))) | |
print("Convert set to list:", timeit(converter(l ,s))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment