2016-11-21 73 views
9

我想比較不同,從不同的變量建立在Python的字符串:使用+來連接Python字符串格式化:'%'比'格式'功能更有效嗎?

  • (簡稱 '加號')使用format使用"".join(list)
  • 使用%
  • 功能
  • 使用"{0.<attribute>}".format(object)

我爲3種類型的scenari的

  • 串與2個變量
  • 相比
  • 串有4個變量
  • 串有4個變量,每個變量使用兩次

予測得的各1個百萬次操作時間和平均執行6個措施。我想出了以下時序:

Timings

在每一個場景,我想出了以下結論

  • 級聯似乎是格式化使用%是最快的方法
  • 之一比用format函數格式化快得多

我認爲format%(例如在this question)和%幾乎被棄用。

因此我幾個問題:

  1. %真的比format快?
  2. 如果是這樣,爲什麼呢?
  3. 爲什麼"{} {}".format(var1, var2)"{0.attribute1} {0.attribute2}".format(object)更高效?

作爲參考,我用下面的代碼來測量不同的定時。

import time 
def timing(f, n, show, *args): 
    if show: print f.__name__ + ":\t", 
    r = range(n/10) 
    t1 = time.clock() 
    for i in r: 
     f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); f(*args); f(*args) 
    t2 = time.clock() 
    timing = round(t2-t1, 3) 
    if show: print timing 
    return timing 


#Class 
class values(object): 
    def __init__(self, a, b, c="", d=""): 
     self.a = a 
     self.b = b 
     self.c = c 
     self.d = d 


def test_plus(a, b): 
    return a + "-" + b 

def test_percent(a, b): 
    return "%s-%s" % (a, b) 

def test_join(a, b): 
    return ''.join([a, '-', b]) 

def test_format(a, b): 
    return "{}-{}".format(a, b) 

def test_formatC(val): 
    return "{0.a}-{0.b}".format(val) 


def test_plus_long(a, b, c, d): 
    return a + "-" + b + "-" + c + "-" + d 

def test_percent_long(a, b, c, d): 
    return "%s-%s-%s-%s" % (a, b, c, d) 

def test_join_long(a, b, c, d): 
    return ''.join([a, '-', b, '-', c, '-', d]) 

def test_format_long(a, b, c, d): 
    return "{0}-{1}-{2}-{3}".format(a, b, c, d) 

def test_formatC_long(val): 
    return "{0.a}-{0.b}-{0.c}-{0.d}".format(val) 


def test_plus_long2(a, b, c, d): 
    return a + "-" + b + "-" + c + "-" + d + "-" + a + "-" + b + "-" + c + "-" + d 

def test_percent_long2(a, b, c, d): 
    return "%s-%s-%s-%s-%s-%s-%s-%s" % (a, b, c, d, a, b, c, d) 

def test_join_long2(a, b, c, d): 
    return ''.join([a, '-', b, '-', c, '-', d, '-', a, '-', b, '-', c, '-', d]) 

def test_format_long2(a, b, c, d): 
    return "{0}-{1}-{2}-{3}-{0}-{1}-{2}-{3}".format(a, b, c, d) 

def test_formatC_long2(val): 
    return "{0.a}-{0.b}-{0.c}-{0.d}-{0.a}-{0.b}-{0.c}-{0.d}".format(val) 


def test_plus_superlong(lst): 
    string = "" 
    for i in lst: 
     string += str(i) 
    return string 


def test_join_superlong(lst): 
    return "".join([str(i) for i in lst]) 


def mean(numbers): 
    return float(sum(numbers))/max(len(numbers), 1) 


nb_times = int(1e6) 
n = xrange(5) 
lst_numbers = xrange(1000) 
from collections import defaultdict 
metrics = defaultdict(list) 
list_functions = [ 
    test_plus, test_percent, test_join, test_format, test_formatC, 
    test_plus_long, test_percent_long, test_join_long, test_format_long, test_formatC_long, 
    test_plus_long2, test_percent_long2, test_join_long2, test_format_long2, test_formatC_long2, 
    # test_plus_superlong, test_join_superlong, 
] 
val = values("123", "456", "789", "0ab") 
for i in n: 
    for f in list_functions: 
     print ".", 
     name = f.__name__ 
     if "formatC" in name: 
      t = timing(f, nb_times, False, val) 
     elif '_long' in name: 
      t = timing(f, nb_times, False, "123", "456", "789", "0ab") 
     elif '_superlong' in name: 
      t = timing(f, nb_times, False, lst_numbers) 
     else: 
      t = timing(f, nb_times, False, "123", "456") 
     metrics[name].append(t) 

#Get Average 
print "\n===AVERAGE OF TIMINGS===" 
for f in list_functions: 
    name = f.__name__ 
    timings = metrics[name] 
    print "{:>20}:\t{:0.5f}".format(name, mean(timings)) 
+1

使用'timeit'代替你的自定義函數,可能第一次執行速度慢,但後續函數執行速度更快,但實際上你只能調用一次函數。 https://docs.python.org/2/library/timeit.html –

+0

正如@MaximilianPeters所說,你應該使用'timeit'來獲得值得信賴的結果 –

+0

謝謝你們。我檢查了'timeit',但我應該一直很高,因爲我相信它只支持Python 3.x,我主要使用2.7。 –

回答

8
  1. 是,%字符串格式化比.format方法快
  2. 最有可能的(這可能有一個更好的解釋)由於%是一個語法符號(因此執行速度快),而.format涉及至少一個額外的方法調用
  3. 因爲屬性值訪問還涉及一個額外的方法調用, __getattr__

我跑使用各種格式化方法,結果可放置timeit如下稍好的分析(關於Python 3.6.0)(美麗的印刷用BeautifulTable) -

 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| Type \ num_vars | 1 | 2 | 5 | 10 | 50 | 250 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| f_str_str | 0.306 | 0.064 | 0.106 | 0.183 | 0.737 | 3.422 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| f_str_int | 0.295 | 0.174 | 0.385 | 0.686 | 3.378 | 16.399 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| concat_str | 0.012 | 0.053 | 0.156 | 0.31 | 1.707 | 16.762 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| pct_s_str | 0.056 | 0.178 | 0.275 | 0.469 | 1.872 | 9.191 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| pct_s_int | 0.128 | 0.208 | 0.343 | 0.605 | 2.483 | 13.24 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| dot_format_str | 0.418 | 0.217 | 0.343 | 0.58 | 2.241 | 11.163 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| dot_format_int | 0.416 | 0.277 | 0.476 | 0.811 | 3.378 | 17.829 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| dot_format2_str | 0.433 | 0.242 | 0.416 | 0.675 | 3.152 | 16.783 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 
| dot_format2_int | 0.428 | 0.298 | 0.541 | 0.933 | 4.444 | 24.767 | 
+-----------------+-------+-------+-------+-------+-------+--------+ 

尾隨_str & _int表示操作是在各個值類型上執行的。

我在結果到達設置 -

times = {} 

for num_vars in (1, 2, 5, 10, 50, 250): 
    f_str = "f'{" + '}{'.join([f'x{i}' for i in range(num_vars)]) + "}'" 
    # "f'{x0}{x1}" 
    concat = '+'.join([f'x{i}' for i in range(num_vars)]) 
    # 'x0+x1' 
    pct_s = '"' + '%s'*num_vars + '" % (' + ','.join([f'x{i}' for i in range(num_vars)]) + ')' 
    # '"%s%s" % (x0,x1)' 
    dot_format = '"' + '{}'*num_vars + '".format(' + ','.join([f'x{i}' for i in range(num_vars)]) + ')' 
    # '"{}{}".format(x0,x1)' 
    dot_format2 = '"{' + '}{'.join([f'{i}' for i in range(num_vars)]) + '}".format(' + ','.join([f'x{i}' for i in range(num_vars)]) + ')' 
    # '"{0}{1}".format(x0,x1)' 

    vars = ','.join([f'x{i}' for i in range(num_vars)]) 
    vals_str = tuple(map(str, range(num_vars))) 
    setup_str = f'{vars} = {vals_str}' 
    # "x0,x1 = ('0', '1')" 
    vals_int = tuple(range(num_vars)) 
    setup_int = f'{vars} = {vals_int}' 
    # 'x0,x1 = (0, 1)' 

    times[num_vars] = { 
     'f_str_str': timeit(f_str, setup_str), 
     'f_str_int': timeit(f_str, setup_int), 
     'concat_str': timeit(concat, setup_str), 
     # 'concat_int': timeit(concat, setup_int), # this will be summation, not concat 
     'pct_s_str': timeit(pct_s, setup_str), 
     'pct_s_int': timeit(pct_s, setup_int), 
     'dot_format_str': timeit(dot_format, setup_str), 
     'dot_format_int': timeit(dot_format, setup_int), 
     'dot_format2_str': timeit(dot_format2, setup_str), 
     'dot_format2_int': timeit(dot_format2, setup_int), 
    } 

table = BeautifulTable() 
table.column_headers = ['Type \ num_vars'] + list(map(str, times.keys())) 
# Order is preserved, so I didn't worry much 
for key in ('f_str_str', 'f_str_int', 'concat_str', 'pct_s_str', 'pct_s_int', 'dot_format_str', 'dot_format_int', 'dot_format2_str', 'dot_format2_int'): 
    table.append_row([key] + [times[num_vars][key] for num_vars in (1, 2, 5, 10, 50, 250)]) 
print(table) 

我無法超越num_vars=250因爲一些max參數(255)與timeit限制。

TL;博士 - Python字符串格式化性能:f-strings是最快的,更優雅,但有時​​(由於一些implementation restrictions &是Py3.6 +只),您可能需要使用其他格式選項。

+0

偉大的答案,偉大的代碼。謝謝! –