Created
August 20, 2018 00:22
-
-
Save StrikingLoo/202f7c6ffb88113e9fe8774c75d98a3d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import time | |
| BIG = 20000000 | |
| def f(k): | |
| return 2*k | |
| def list_a(): | |
| list_a = [] | |
| for i in range(BIG): | |
| list_a.append(f(i)) | |
| def list_b(): | |
| list_b = [f(i) for i in range(BIG)] | |
| def list_a_filtered(): | |
| list_a = [] | |
| for i in range(BIG): | |
| if i%2==0: | |
| list_a.append(f(i)) | |
| def list_b_filtered(): | |
| list_b = [f(i) for i in range(BIG) if i%2==0] | |
| def benchmark(function, function_name): | |
| start = time.time() | |
| function() | |
| end = time.time() | |
| print("{0} seconds for {1}".format((end - start), function_name)) | |
| benchmark(list_a, "list a") | |
| benchmark(list_b, "list b") | |
| benchmark(list_a_filtered, "filtered list a") | |
| benchmark(list_b_filtered, "filtered list b") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment