Forked from jdhao/benchmark_lambda_itemgetter.py
Last active
February 26, 2019 17:18
-
-
Save futska/02cb445b96a10e805cbbd5ae43e6b094 to your computer and use it in GitHub Desktop.
Benchmark result of lambda and itemgetter used in sort method in Python
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
""" | |
Description: In this script, I plot the benchmark result of lambda compared | |
to itemgetter in the operator package. We sort a list of tuple (which has two | |
elements) to benchmark. The list element number ranges from 100 to 1000000. | |
""" | |
# import numpy as np | |
import matplotlib | |
import matplotlib.pyplot as plt | |
colors = ["#e6194b", | |
"#3cb44b"] | |
# the number of tuple in list | |
N = [100, 1000, 10000, 100000, 1000000] | |
# time for lambda | |
t1 = [8.19, 81.4, 855, 14600, 172000] | |
# time for itemgetter | |
t2 = [5.09, 47, 498, 10100, 131000] | |
fig, ax = plt.subplots() | |
ax.semilogx(N, t1, marker='.', label="lambda") | |
ax.semilogx(N, t2, marker='.', label="itemgetter") | |
ax.set_xlabel("The number of elements in list") | |
ax.set_ylabel("Time to sort the list " + r"(in $\mu s$)") | |
ax.set_xticks(N) | |
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) | |
ax.legend() | |
# use the following method to turn off minor ticks | |
ax.tick_params(axis='x', which='minor', bottom='off') | |
# ax.minorticks_off() # or use minorticks_off method | |
# turn grid on | |
ax.grid(linestyle='--') | |
plt.show() |
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
a = list(range(1000)) | |
b = list(range(1000)) | |
import random | |
random.shuffle(a) | |
random.shuffle(b) | |
c = list(zip(a, b)) | |
%timeit c.sort(key=lambda x: x[1]) | |
# 81.4 µs ± 433 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) | |
random.shuffle(c) | |
from operator import itemgetter | |
%timeit c.sort(key=itemgetter(1)) | |
# 47 µs ± 202 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each) | |
I have also tested the performance (run time in µs) of this two method for various list size. | |
+-----------+--------+------------+ | |
| List size | lambda | itemgetter | | |
+-----------+--------+------------+ | |
| 100 | 8.19 | 5.09 | | |
+-----------+--------+------------+ | |
| 1000 | 81.4 | 47 | | |
+-----------+--------+------------+ | |
| 10000 | 855 | 498 | | |
+-----------+--------+------------+ | |
| 100000 | 14600 | 10100 | | |
+-----------+--------+------------+ | |
| 1000000 | 172000 | 131000 | | |
+-----------+--------+------------+ | |
https://gist.github.com/jdhao/5569afa7efc13abf75a5baf18e7c29d6 for graph |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment