Почему% s быстрее, чем% d для подстановки целых чисел в Python?

I am going through the examples mentioned here, and am looking at this example. I ran a sample example below on ipython, and the result is consistent, i.e., "%d" is slower than "%s":

In [1]: def m1():
   ...:     return "%d" % (2*3/5)

In [2]: def m2():
   ...:     return "%s" % (2*3/5)

In [4]: %timeit m1()
1000000 loops, best of 3: 529 ns per loop

In [5]: %timeit m2()
1000000 loops, best of 3: 192 ns per loop

In [6]: from dis import dis

In [7]: dis(m1)
  2           0 LOAD_CONST               1 ('%d')
              3 LOAD_CONST               5 (6)
              6 LOAD_CONST               4 (5)
              9 BINARY_DIVIDE       
             10 BINARY_MODULO       
             11 RETURN_VALUE        

In [9]: dis(m2)
  2           0 LOAD_CONST               1 ('%s')
              3 LOAD_CONST               5 (6)
              6 LOAD_CONST               4 (5)
              9 BINARY_DIVIDE       
             10 BINARY_MODULO       
             11 RETURN_VALUE        

Both the code blocks are similar, and even the output of disassembler is same, so why is "%s" faster than "%d"?

7 голосов | спросил mu 無 6 Jam1000000amTue, 06 Jan 2015 10:33:02 +030015 2015, 10:33:02

0 ответов


Похожие вопросы

Популярные теги

security × 330linux × 316macos × 2827 × 268performance × 244command-line × 241sql-server × 235joomla-3.x × 222java × 189c++ × 186windows × 180cisco × 168bash × 158c# × 142gmail × 139arduino-uno × 139javascript × 134ssh × 133seo × 132mysql × 132