|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Created by imoyao at 2019/5/15 11:04 |
| 4 | +""" |
| 5 | +列表操作及性能测试 |
| 6 | +""" |
| 7 | +import pathlib |
| 8 | +import sys |
| 9 | +import timeit |
| 10 | + |
| 11 | +util_p = pathlib.Path('../..').resolve() |
| 12 | +sys.path.append(str(util_p)) |
| 13 | +from util import utils |
| 14 | + |
| 15 | + |
| 16 | +@utils.show_time |
| 17 | +def list_plus(n): |
| 18 | + a_list = [] |
| 19 | + for i in range(n): |
| 20 | + a_list += [i] |
| 21 | + return a_list |
| 22 | + |
| 23 | + |
| 24 | +@utils.show_time |
| 25 | +def list_append(n): |
| 26 | + a_list = [] |
| 27 | + for i in range(n): |
| 28 | + a_list.append(i) |
| 29 | + return a_list |
| 30 | + |
| 31 | + |
| 32 | +@utils.show_time |
| 33 | +def list_expression(n): |
| 34 | + return [_ for _ in range(n)] |
| 35 | + |
| 36 | + |
| 37 | +# @utils.show_time |
| 38 | +def list_range(n): |
| 39 | + return list(range(n)) |
| 40 | + |
| 41 | + |
| 42 | +if __name__ == '__main__': |
| 43 | + # t1 = timeit.Timer('list_plus(1000)', 'from __main__ import list_plus', ) |
| 44 | + # print(f'{list_plus.__name__} takes {t1.timeit(number=1000)} ms.') |
| 45 | + # |
| 46 | + # t2 = timeit.Timer('list_append(1000)', 'from __main__ import list_append') |
| 47 | + # print(f'{list_append.__name__} takes {t1.timeit(number=1000)} ms.') |
| 48 | + # |
| 49 | + # t3 = timeit.Timer('list_expression(1000)', 'from __main__ import list_expression') |
| 50 | + # print(f'{list_expression.__name__} takes {t1.timeit(number=1000)} ms.') |
| 51 | + # |
| 52 | + # t4 = timeit.Timer('list_range(1000)', 'from __main__ import list_range') |
| 53 | + # print(f'{list_range.__name__} takes {t1.timeit(number=1000)} ms.') |
| 54 | + |
| 55 | + # 上方为使用timeit模块的测试,下方为使用自写装饰器的测试 |
| 56 | + |
| 57 | + n = 1000000 |
| 58 | + list_plus(n) |
| 59 | + list_append(n) |
| 60 | + list_expression(n) |
| 61 | + list_range(n) |
| 62 | + ''' |
| 63 | + # 输出 |
| 64 | + The function **list_plus** takes 0.47864699363708496 time. |
| 65 | + The function **list_append** takes 0.41912221908569336 time. |
| 66 | + The function **list_expression** takes 0.14060020446777344 time. |
| 67 | + The function **list_range** takes 0.06196928024291992 time. |
| 68 | + ''' |
| 69 | + |
| 70 | + # 两种不同方式pop()操作耗时对比 |
| 71 | + |
| 72 | + n = 10000 |
| 73 | + x = list_range(n) |
| 74 | + p1 = timeit.Timer('x.pop()', 'from __main__ import x') |
| 75 | + print(f'list_pop_normal takes {p1.timeit(number=1000)} ms.') |
| 76 | + |
| 77 | + p2 = timeit.Timer('x.pop(0)', 'from __main__ import x') |
| 78 | + print(f'list_pop_index takes {p2.timeit(number=1000)} ms.') |
| 79 | + ''' |
| 80 | + 对比两次,发现指定index时耗时会随着list的增大而增加 |
| 81 | + n = 1000000 |
| 82 | + list_pop_normal takes 0.0006615779711864889 ms. |
| 83 | + list_pop_index takes 1.150215208006557 ms. |
| 84 | + # n = 10000 |
| 85 | + list_pop_normal takes 0.00041822699131444097 ms. |
| 86 | + list_pop_index takes 0.0079622509656474 ms. |
| 87 | + ''' |
0 commit comments