gpt4 book ai didi

python - 为什么用扩展切片反转字符串这么快?

转载 作者:太空宇宙 更新时间:2023-11-04 04:10:54 25 4
gpt4 key购买 nike

我只是timed a couple of string reversal methods当然,扩展切片非常快。事实上,使用 Python 3.6.7,我的印象是它有恒定的时间:

enter image description here

20 chars            : min:   0.6μs, mean:   0.7μs, max:    3.7μs
2000 chars : min: 0.5μs, mean: 0.6μs, max: 5.8μs
200000 chars : min: 0.5μs, mean: 0.6μs, max: 2.5μs
200000000 chars : min: 0.5μs, mean: 0.6μs, max: 2.5μs

为什么会这样?我假设它至少需要遍历所有元素并因此具有线性时间?是否涉及 cPython 的一些指针魔法?我的评估有错误吗?

代码

#!/usr/bin/env python

import numpy as np
import random
import timeit
random.seed(0)


def main():
string_20 = ''.join(random.choices("ABCDEFGHIJKLM", k=20))
string_2000 = ''.join(random.choices("ABCDEFGHIJKLM", k=2000))
string_200000 = ''.join(random.choices("ABCDEFGHIJKLM", k=200000))
string_200000000 = ''.join(random.choices("ABCDEFGHIJKLM", k=200000000))
functions = [(list_comprehension, '20 chars', string_20),
(list_comprehension, '2000 chars', string_2000),
(list_comprehension, '200000 chars', string_200000),
(list_comprehension, '200000000 chars', string_200000000),
]
duration_list = {}
for func, name, params in functions:
durations = timeit.repeat(lambda: func(params), repeat=100, number=3)
duration_list[name] = list(np.array(durations) * 1000)
print('{func:<20}: '
'min: {min:5.1f}μs, mean: {mean:5.1f}μs, max: {max:6.1f}μs'
.format(func=name,
min=min(durations) * 10**6,
mean=np.mean(durations) * 10**6,
max=max(durations) * 10**6,
))
create_boxplot('Reversing a string of various lengths', duration_list)


def list_comprehension(string):
return string[::1]


def create_boxplot(title, duration_list, showfliers=False):
import seaborn as sns
import matplotlib.pyplot as plt
import operator
plt.figure(num=None, figsize=(8, 4), dpi=300,
facecolor='w', edgecolor='k')
sns.set(style="whitegrid")
sorted_keys, sorted_vals = zip(*duration_list.items())
flierprops = dict(markerfacecolor='0.75', markersize=1,
linestyle='none')
ax = sns.boxplot(data=sorted_vals, width=.3, orient='h',
flierprops=flierprops,
showfliers=showfliers)
ax.set(xlabel="Time in ms", ylabel="")
plt.yticks(plt.yticks()[0], sorted_keys)
ax.set_title(title)
plt.tight_layout()
plt.savefig("output-string-list-comp.png")


if __name__ == '__main__':
main()

最佳答案

刚刚查了一下。 list_comprehension方法必须是

def list_comprehension(string):
return string[::-1]

我遗漏了最大的列表,因为它花了很长时间。这是我的输出:

/usr/bin/python3 ./boxplot.py 
20 chars : min: 1.7μs, mean: 2.0μs, max: 7.5μs
2000 chars : min: 6.7μs, mean: 6.8μs, max: 13.4μs
200000 chars : min: 567.6μs, mean: 609.9μs, max: 997.2μs

似乎不像预期的那样恒定:-)

enter image description here

顺便说一句,箱线图不错!

关于python - 为什么用扩展切片反转字符串这么快?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56282893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com