Skip to content

Instantly share code, notes, and snippets.

@bootjp
Last active May 17, 2020 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bootjp/29cfa05235d6db1ab2bf6087b16d6e9b to your computer and use it in GitHub Desktop.
Save bootjp/29cfa05235d6db1ab2bf6087b16d6e9b to your computer and use it in GitHub Desktop.
python list vs. set add and remove opration.
import time
sset = set()
arr = []
def main():
for _ in range(3):
start = time.time()
for i in range(50):
for i in range(50):
arr.append(i)
for ii in range(50):
if ii in arr:
arr.remove(ii)
elapsed_time = time.time() - start
print("list for elapsed_time:{0}".format(elapsed_time) + "[sec]")
start = time.time()
for _ in range(50):
for i in range(50):
sset.add(i)
for ii in range(50):
sset.discard(ii)
elapsed_time = time.time() - start
print("set for elapsed_time:{0}".format(elapsed_time) + "[sec]")
print("--")
if __name__ == "__main__":
main()
@bootjp
Copy link
Author

bootjp commented May 17, 2020

result

bootjp@bootjpnoMBP ~/b/vrc_meta_tool (master)> python3 ./bench.py
list for elapsed_time:0.0005528926849365234[sec]
set for  elapsed_time:0.0005402565002441406[sec]
--
list for elapsed_time:0.0005221366882324219[sec]
set for  elapsed_time:0.0007278919219970703[sec]
--
list for elapsed_time:0.0009663105010986328[sec]
set for  elapsed_time:0.0007507801055908203[sec]
--

@bootjp
Copy link
Author

bootjp commented May 17, 2020

try 5000 loops

bootjp@bootjpnoMBP ~/b/vrc_meta_tool (master)> python3 ./bench.py
list for elapsed_time:15.734405040740967[sec]
set for  elapsed_time:6.3566577434539795[sec]
--
list for elapsed_time:13.07068681716919[sec]
set for  elapsed_time:6.044255018234253[sec]
--
list for elapsed_time:17.666430234909058[sec]
set for  elapsed_time:6.462040901184082[sec]
--

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment