Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Created February 24, 2014 09:19
Show Gist options
  • Select an option

  • Save ksomemo/9184385 to your computer and use it in GitHub Desktop.

Select an option

Save ksomemo/9184385 to your computer and use it in GitHub Desktop.
import time
range_end = 1000000
search_val = range_end - 1
my_list = range(1, range_end)
my_set = set(my_list)
my_dict = dict.fromkeys(my_list)
def test_list():
if search_val in my_list:
pass
def test_set():
if search_val in my_set:
pass
def test_dict():
if my_dict.has_key(search_val):
pass
def test_collection(func):
print func
start = time.clock()
try_cnt = 10
for i in range(try_cnt):
func()
end = time.clock()
print (end - start) / try_cnt
if __name__ == '__main__':
test_collection(test_list)
test_collection(test_set)
test_collection(test_dict)
@ksomemo
Copy link
Author

ksomemo commented Feb 24, 2014

C:\Python27\python.exe list_set_dict_compare.py
function test_list at 0x021059B0
0.0212700972286
function test_set at 0x021059F0
1.1084162456e-06
function test_dict at 0x02105970
7.38944163736e-07

Process finished with exit code 0

関数表示時の<>を削除している

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