Tôi đã thực hiện một thử nghiệm nhỏ để xem phương pháp nào trong số này
string.startswith['hello']
string.rfind['hello'] == 0
string.rpartition['hello'][0] == ''
string.rindex['hello'] == 0
hiệu quả nhất để trả về xem một chuỗi nhất định bắt đầu bằng một chuỗi khác.
Đây là kết quả của một trong nhiều lần thử nghiệm mà tôi đã thực hiện, trong đó mỗi danh sách được yêu cầu hiển thị ít nhất thời gian [trong vài giây] để phân tích 5 triệu của mỗi biểu thức trên trong mỗi lần lặp của vòng lặp while
i đã sử dụng:
['startswith: 1.37', 'rpartition: 1.38', 'rfind: 1.62', 'rindex: 1.62']
['startswith: 1.28', 'rpartition: 1.44', 'rindex: 1.67', 'rfind: 1.68']
['startswith: 1.29', 'rpartition: 1.42', 'rindex: 1.63', 'rfind: 1.64']
['startswith: 1.28', 'rpartition: 1.43', 'rindex: 1.61', 'rfind: 1.62']
['rpartition: 1.48', 'startswith: 1.48', 'rfind: 1.62', 'rindex: 1.67']
['startswith: 1.34', 'rpartition: 1.43', 'rfind: 1.64', 'rindex: 1.64']
['startswith: 1.36', 'rpartition: 1.44', 'rindex: 1.61', 'rfind: 1.63']
['startswith: 1.29', 'rpartition: 1.37', 'rindex: 1.64', 'rfind: 1.67']
['startswith: 1.34', 'rpartition: 1.44', 'rfind: 1.66', 'rindex: 1.68']
['startswith: 1.44', 'rpartition: 1.41', 'rindex: 1.61', 'rfind: 2.24']
['startswith: 1.34', 'rpartition: 1.45', 'rindex: 1.62', 'rfind: 1.67']
['startswith: 1.34', 'rpartition: 1.38', 'rindex: 1.67', 'rfind: 1.74']
['rpartition: 1.37', 'startswith: 1.38', 'rfind: 1.61', 'rindex: 1.64']
['startswith: 1.32', 'rpartition: 1.39', 'rfind: 1.64', 'rindex: 1.61']
['rpartition: 1.35', 'startswith: 1.36', 'rfind: 1.63', 'rindex: 1.67']
['startswith: 1.29', 'rpartition: 1.36', 'rfind: 1.65', 'rindex: 1.84']
['startswith: 1.41', 'rpartition: 1.44', 'rfind: 1.63', 'rindex: 1.71']
['startswith: 1.34', 'rpartition: 1.46', 'rindex: 1.66', 'rfind: 1.74']
['startswith: 1.32', 'rpartition: 1.46', 'rfind: 1.64', 'rindex: 1.74']
['startswith: 1.38', 'rpartition: 1.48', 'rfind: 1.68', 'rindex: 1.68']
['startswith: 1.35', 'rpartition: 1.42', 'rfind: 1.63', 'rindex: 1.68']
['startswith: 1.32', 'rpartition: 1.46', 'rfind: 1.65', 'rindex: 1.75']
['startswith: 1.37', 'rpartition: 1.46', 'rfind: 1.74', 'rindex: 1.75']
['startswith: 1.31', 'rpartition: 1.48', 'rfind: 1.67', 'rindex: 1.74']
['startswith: 1.44', 'rpartition: 1.46', 'rindex: 1.69', 'rfind: 1.74']
['startswith: 1.44', 'rpartition: 1.42', 'rfind: 1.65', 'rindex: 1.65']
['startswith: 1.36', 'rpartition: 1.44', 'rfind: 1.64', 'rindex: 1.74']
['startswith: 1.34', 'rpartition: 1.46', 'rfind: 1.61', 'rindex: 1.74']
['startswith: 1.35', 'rpartition: 1.56', 'rfind: 1.68', 'rindex: 1.69']
['startswith: 1.32', 'rpartition: 1.48', 'rindex: 1.64', 'rfind: 1.65']
['startswith: 1.28', 'rpartition: 1.43', 'rfind: 1.59', 'rindex: 1.66']
Tôi tin rằng điều khá rõ ràng ngay từ đầu rằng phương pháp
from time import perf_counter
string = 'hello world'
places = dict[]
while True:
start = perf_counter[]
for _ in range[5000000]:
string.startswith['hello']
end = perf_counter[]
places['startswith'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rfind['hello'] == 0
end = perf_counter[]
places['rfind'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rpartition['hello'][0] == ''
end = perf_counter[]
places['rpartition'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rindex['hello'] == 0
end = perf_counter[]
places['rindex'] = round[end - start, 2]
print[[f'{b}: {str[a].ljust[4, "4"]}' for a, b in sorted[i[::-1] for i in places.items[]]]]
0 sẽ xuất hiện hiệu quả nhất, vì trả về liệu một chuỗi bắt đầu với chuỗi được chỉ định là mục đích chính của nó.Điều làm tôi ngạc nhiên là phương pháp string.rpartition['hello'][0] == ''
dường như không thực tế luôn tìm ra cách được liệt kê trước, trước phương pháp string.startswith['hello']
, mọi lúc mọi nơi. Kết quả cho thấy rằng sử dụng
from time import perf_counter
string = 'hello world'
places = dict[]
while True:
start = perf_counter[]
for _ in range[5000000]:
string.startswith['hello']
end = perf_counter[]
places['startswith'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rfind['hello'] == 0
end = perf_counter[]
places['rfind'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rpartition['hello'][0] == ''
end = perf_counter[]
places['rpartition'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rindex['hello'] == 0
end = perf_counter[]
places['rindex'] = round[end - start, 2]
print[[f'{b}: {str[a].ljust[4, "4"]}' for a, b in sorted[i[::-1] for i in places.items[]]]]
3 để xác định xem một chuỗi bắt đầu với một chuỗi khác có hiệu quả hơn hay không thì sử dụng cả from time import perf_counter
string = 'hello world'
places = dict[]
while True:
start = perf_counter[]
for _ in range[5000000]:
string.startswith['hello']
end = perf_counter[]
places['startswith'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rfind['hello'] == 0
end = perf_counter[]
places['rfind'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rpartition['hello'][0] == ''
end = perf_counter[]
places['rpartition'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rindex['hello'] == 0
end = perf_counter[]
places['rindex'] = round[end - start, 2]
print[[f'{b}: {str[a].ljust[4, "4"]}' for a, b in sorted[i[::-1] for i in places.items[]]]]
4 và from time import perf_counter
string = 'hello world'
places = dict[]
while True:
start = perf_counter[]
for _ in range[5000000]:
string.startswith['hello']
end = perf_counter[]
places['startswith'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rfind['hello'] == 0
end = perf_counter[]
places['rfind'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rpartition['hello'][0] == ''
end = perf_counter[]
places['rpartition'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rindex['hello'] == 0
end = perf_counter[]
places['rindex'] = round[end - start, 2]
print[[f'{b}: {str[a].ljust[4, "4"]}' for a, b in sorted[i[::-1] for i in places.items[]]]]
5.Một điều nữa tôi nhận thấy là string.rfind['hello'] == 0
và string.rindex['hello'] == 0
có một trận chiến tốt đang diễn ra, mỗi lần tăng từ vị trí thứ tư sang vị trí thứ ba, và giảm từ vị trí thứ ba xuống thứ tư, điều này có ý nghĩa, vì mục đích chính của họ là như nhau.
Đây là mã:
from time import perf_counter
string = 'hello world'
places = dict[]
while True:
start = perf_counter[]
for _ in range[5000000]:
string.startswith['hello']
end = perf_counter[]
places['startswith'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rfind['hello'] == 0
end = perf_counter[]
places['rfind'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rpartition['hello'][0] == ''
end = perf_counter[]
places['rpartition'] = round[end - start, 2]
start = perf_counter[]
for _ in range[5000000]:
string.rindex['hello'] == 0
end = perf_counter[]
places['rindex'] = round[end - start, 2]
print[[f'{b}: {str[a].ljust[4, "4"]}' for a, b in sorted[i[::-1] for i in places.items[]]]]
Sự mô tả
Phương thức chuỗi Python startSwith [] kiểm tra xem chuỗi bắt đầu bằng str, tùy chọn hạn chế khớp với các chỉ số đã cho bắt đầu và kết thúc.startswith[] checks whether string starts with str, optionally restricting the matching with the given indices start and end.
Cú pháp
Sau đây là cú pháp cho phương thức startSwith [] -startswith[] method −
str.startswith[str, beg=0,end=len[string]];
Thông số
STR - đây là chuỗi cần kiểm tra. − This is the string to be checked.
Beg - Đây là tham số tùy chọn để đặt chỉ mục bắt đầu của ranh giới khớp. − This is the optional parameter to set start index of the matching boundary.
Kết thúc - Đây là tham số tùy chọn để kết thúc chỉ số bắt đầu của ranh giới khớp. − This is the optional parameter to end start index of the matching boundary.
Giá trị trả về
Phương thức này trả về true nếu tìm thấy chuỗi khớp nếu không sai.
Thí dụ
Ví dụ sau đây cho thấy việc sử dụng phương thức startSwith [].
#!/usr/bin/python str = "this is string example....wow!!!"; print str.startswith[ 'this' ] print str.startswith[ 'is', 2, 4 ] print str.startswith[ 'this', 2, 4 ]
Khi chúng tôi chạy trên chương trình, nó tạo ra kết quả sau -
True True False
python_strings.htm