import time
import numpy as np
import matplotlib.pyplot as plt
tt =[]
n = 100000
t = [[],[],[]]
for j in range(5):
a = 'กข'
b = 'คง'
t0 = time.time()
for i in range(n):
'%s%s'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{}{}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a}{b}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = 10
b = 200
t0 = time.time()
for i in range(n):
'%d%d'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{}{}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a}{b}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = 10
b = 200
t0 = time.time()
for i in range(n):
'%010d%010d'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{:010}{:010}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a:010}{b:010}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = 1.522555
b = 3.125
t0 = time.time()
for i in range(n):
'%e%e'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{:e}{:e}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a:e}{b:e}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = 1.522555
b = 3.125
t0 = time.time()
for i in range(n):
'%.10e%.10e'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{:.10e}{:.10e}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a:.10e}{b:.10e}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = 1.522555
b = 3.125
t0 = time.time()
for i in range(n):
'%f%f'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{:f}{:f}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a:f}{b:f}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = 1.522555
b = 3.125
t0 = time.time()
for i in range(n):
'%.10f%.10f'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{:.10f}{:.10f}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a:.10f}{b:.10f}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = 1.522555
b = 3.125
t0 = time.time()
for i in range(n):
'%s%s'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{}{}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a}{b}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = None
b = None
t0 = time.time()
for i in range(n):
'%s%s'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{}{}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a}{b}'
t[2].append(time.time()-t0)
tt.append(t)
t = [[],[],[]]
for j in range(5):
a = list(range(1,6))
b = list(range(11,16))
t0 = time.time()
for i in range(n):
'%s%s'%(a, b)
t[0].append(time.time()-t0)
t0 = time.time()
for i in range(n):
'{}{}'.format(a, b)
t[1].append(time.time()-t0)
t0 = time.time()
for i in range(n):
f'{a}{b}'
t[2].append(time.time()-t0)
tt.append(t)
tt = np.array(tt)/n*1e6
plt.figure(figsize=[6.5,6])
plt.subplot(211)
for i,c in enumerate(['#ffaaaa','#aaffaa','#aaaaff']):
plt.bar(np.arange(10)+(i-1)/4.,tt[:,i].mean(1),width=0.25,yerr=tt[:,i].std(1),capsize=4,color=c,ecolor='#332255')
plt.legend(['ใช้ %','ใช้ format','ใช้ f-string'],prop={'family':'Tahoma'})
plt.ylabel('เวลา (ไมโครวินาที)',family='Tahoma')
plt.xticks([])
plt.subplot(212)
for i,c in enumerate(['#ffaaaa','#aaffaa','#aaaaff']):
plt.bar(np.arange(10)+(i-1)/4.,(tt[:,i]/tt[:,0]).mean(1),width=0.25,yerr=(tt[:,i]/tt[:,0]).std(1),capsize=5,color=c,ecolor='#332211')
plt.ylabel('จำนวนเท่าของแบบใช้ %',family='Tahoma')
plt.xticks(np.arange(10),['s','d','010d','e','.10e','f','.10f','f>>s','None','list'])
plt.tight_layout()
plt.show()
a = 123456789
print(f'{a:,}') # ได้ 123,456,789
ติดตามอัปเดตของบล็อกได้ที่แฟนเพจ