Comparison of three characters to determine whether the way of efficiency in capital letters

Chat with friends and talk to the judge character is an uppercase letter, written so readily look a bit more time-consuming statistics.
Think of three ways:

## 方法A
x in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' #如果返回Ture则x为大写

## 方法B
x>='A' and x<='Z' #如果返回Ture则x为大写

## 方法C
x.isupper() #如果返回Ture则x为大写

Which most efficient it? So to some code running about Well ...

from timeit import timeit
import matplotlib.pyplot as plt
%matplotlib inline
## 方法A 
cost_time_A = {}
for xx in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
    cost_time_A[xx] = timeit("'"+xx+"'"+" in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'",number=10000000)
#     print(cost_time_A[xx],xx)
# print('===========')

## 方法B
cost_time_B = {}
for xx in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':  
    cost_time_B[xx] = timeit("'"+xx+"'"+">='A' and "+"'"+xx+"'"+"<='Z'",number=10000000)
#     print(cost_time_B[xx],xx)
# print('===========')

## 方法C
cost_time_C = {}
for xx in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
    cost_time_C[xx] = timeit("'"+xx+"'"+".isupper()",number=10000000)
#     print(cost_time_C[xx],xx)
# print('===========')

### 可视化
plt.ylim(0,1)
plt.plot(cost_time_A.keys(),cost_time_A.values(),label='A')
plt.plot(cost_time_A.keys(),cost_time_B.values(),label='B')
plt.plot(cost_time_A.keys(),cost_time_C.values(),label='C')
plt.legend(loc='best')
plt.show()

## 结论:选用“方法A”,效率更高

Here Insert Picture Description

Conclusion : using the "method A", will be better than the other two!

Guess you like

Origin blog.csdn.net/CallMeYunzi/article/details/83308878