自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

為什么有些時(shí)候 Python 中乘法比位運(yùn)算更快

開發(fā) 后端
某天,一個(gè)技術(shù)群里老哥提出了這樣一個(gè)問題,為什么在一些情況下,Python 中的簡單乘/除法比位運(yùn)算要慢。

我本來以為我不再會寫水文了,但是突然發(fā)現(xiàn)自己現(xiàn)在也只能勉強(qiáng)寫寫水文才能維持生活這樣子。那就繼續(xù)寫水文吧!

某天,一個(gè)技術(shù)群里老哥提出了這樣一個(gè)問題,為什么在一些情況下,Python 中的簡單乘/除法比位運(yùn)算要慢。

首先秉持著實(shí)事求是的精神,我們先來驗(yàn)證一下:

  1. In [33]: %timeit 1073741825*2                                                                                                                                                                                                                                                                            
  2. 7.47 ns ± 0.0843 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each) 
  3.  
  4. In [34]: %timeit 1073741825<<1                                                                                                                                                                                                                                                                           
  5. 7.43 ns ± 0.0451 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each) 
  6.  
  7. In [35]: %timeit 1073741823<<1                                                                                                                                                                                                                                                                           
  8. 7.48 ns ± 0.0621 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each) 
  9.  
  10. In [37]: %timeit 1073741823*2                                                                                                                                                                                                                                                                            
  11. 7.47 ns ± 0.0564 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each) 

我們發(fā)現(xiàn)幾個(gè)很有趣的現(xiàn)象:

  • 在值 x<=2^30 時(shí),乘法比直接位運(yùn)算要快
  • 在值 x>2^32 時(shí),乘法顯著慢于位運(yùn)算

這個(gè)現(xiàn)象很有趣,那么這個(gè)現(xiàn)象的 root cause 是什么?實(shí)際上這和 Python 底層的實(shí)現(xiàn)有關(guān)。

簡單聊聊

1. PyLongObject 的實(shí)現(xiàn)

在 Python 2.x 時(shí)期,Python 中將整型分為兩類,一類是 long, 一類是 int 。在 Python3 中這兩者進(jìn)行了合并。目前在 Python3 中這兩者做了合并,僅剩一個(gè) long。

首先來看看 long 這樣一個(gè)數(shù)據(jù)結(jié)構(gòu)底層的實(shí)現(xiàn):

  1. struct _longobject { 
  2.     PyObject_VAR_HEAD 
  3.     digit ob_digit[1]; 
  4. }; 

在這里不用關(guān)心,PyObject_VAR_HEAD 的含義,我們只需要關(guān)心 ob_digit 即可。

在這里,ob_digit 是使用了 C99 中的“柔性數(shù)組”來實(shí)現(xiàn)任意長度的整數(shù)的存儲。這里我們可以看一下官方代碼中的文檔:

Long integer representation.The absolute value of a number is equal to SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i) Negative numbers are represented with ob_size < 0; zero is represented by ob_size == 0. In a normalized number, ob_digit[abs(ob_size)-1] (the most significant digit) is never zero. Also, in all cases, for all valid i,0 <= ob_digit[i] <= MASK. The allocation function takes care of allocating extra memory so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available. CAUTION: Generic code manipulating subtypes of PyVarObject has to aware that ints abuse ob_size's sign bit.

簡而言之,Python 是將一個(gè)十進(jìn)制數(shù)轉(zhuǎn)為 2^(SHIFT) 進(jìn)制數(shù)來進(jìn)行存儲。這里可能不太好了理解。我來舉個(gè)例子,在我的電腦上,SHIFT 為 30 ,假設(shè)現(xiàn)在有整數(shù) 1152921506754330628 ,那么將其轉(zhuǎn)為 2^30 進(jìn)制表示則為: 4*(2^30)^0+2*(2^30)^1+1*(2^30)^2 。那么此時(shí) ob_digit 是一個(gè)含有三個(gè)元素的數(shù)組,其值為 [4,2,1]。

OK,在明白了這樣一些基礎(chǔ)知識后,我們回過頭去看看 Python 中的乘法運(yùn)算。

2. Python 中的乘法運(yùn)算

Python 中的乘法運(yùn)算,分為兩部分,其中關(guān)于大數(shù)的乘法,Python 使用了 Karatsuba 算法1,具體實(shí)現(xiàn)如下:

  1. static PyLongObject * 
  2. k_mul(PyLongObject *a, PyLongObject *b) 
  3.     Py_ssize_t asize = Py_ABS(Py_SIZE(a)); 
  4.     Py_ssize_t bsize = Py_ABS(Py_SIZE(b)); 
  5.     PyLongObject *ah = NULL
  6.     PyLongObject *al = NULL
  7.     PyLongObject *bh = NULL
  8.     PyLongObject *bl = NULL
  9.     PyLongObject *ret = NULL
  10.     PyLongObject *t1, *t2, *t3; 
  11.     Py_ssize_t shift;           /* the number of digits we split off */ 
  12.     Py_ssize_t i; 
  13.  
  14.     /* (ah*X+al)(bh*X+bl) = ah*bh*X*X + (ah*bl + al*bh)*X + al*bl 
  15.      * Let k = (ah+al)*(bh+bl) = ah*bl + al*bh  + ah*bh + al*bl 
  16.      * Then the original product is 
  17.      *     ah*bh*X*X + (k - ah*bh - al*bl)*X + al*bl 
  18.      * By picking X to be a power of 2, "*X" is just shifting, and it's 
  19.      * been reduced to 3 multiplies on numbers half the size. 
  20.      */ 
  21.  
  22.     /* We want to split based on the larger number; fiddle so that b 
  23.      * is largest. 
  24.      */ 
  25.     if (asize > bsize) { 
  26.         t1 = a
  27.         a = b
  28.         b = t1
  29.  
  30.         i = asize
  31.         asize = bsize
  32.         bsize = i; 
  33.     } 
  34.  
  35.     /* Use gradeschool math when either number is too small. */ 
  36.     i = a == b ? KARATSUBA_SQUARE_CUTOFF : KARATSUBA_CUTOFF; 
  37.     if (asize <= i) { 
  38.         if (asize == 0) 
  39.             return (PyLongObject *)PyLong_FromLong(0); 
  40.         else 
  41.             return x_mul(a, b); 
  42.     } 
  43.  
  44.     /* If a is small compared to b, splitting on b gives a degenerate 
  45.      * case with ah==0, and Karatsuba may be (even much) less efficient 
  46.      * than "grade school" then.  However, we can still win, by viewing 
  47.      * b as a string of "big digits", each of width a->ob_size.  That 
  48.      * leads to a sequence of balanced calls to k_mul. 
  49.      */ 
  50.     if (2 * asize <= bsize) 
  51.         return k_lopsided_mul(a, b); 
  52.  
  53.     /* Split a & b into hi & lo pieces. */ 
  54.     shift = bsize >> 1; 
  55.     if (kmul_split(a, shift, &ah, &al) < 0) goto fail; 
  56.     assert(Py_SIZE(ah) > 0);            /* the split isn't degenerate */ 
  57.  
  58.     if (a == b) { 
  59.         bh = ah
  60.         bl = al
  61.         Py_INCREF(bh); 
  62.         Py_INCREF(bl); 
  63.     } 
  64.     else if (kmul_split(b, shift, &bh, &bl) < 0) goto fail; 
  65.  
  66.     /* The plan: 
  67.      * 1. Allocate result space (asize + bsize digits:  that's always 
  68.      *    enough). 
  69.      * 2. Compute ah*bh, and copy into result at 2*shift. 
  70.      * 3. Compute al*bl, and copy into result at 0.  Note that this 
  71.      *    can't overlap with #2. 
  72.      * 4. Subtract al*bl from the result, starting at shift.  This may 
  73.      *    underflow (borrow out of the high digit), but we don't care: 
  74.      *    we're effectively doing unsigned arithmetic mod 
  75.      *    BASE**(sizea + sizeb), and so long as the *final* result fits, 
  76.      *    borrows and carries out of the high digit can be ignored. 
  77.      * 5. Subtract ah*bh from the result, starting at shift. 
  78.      * 6. Compute (ah+al)*(bh+bl), and add it into the result starting 
  79.      *    at shift. 
  80.      */ 
  81.  
  82.     /* 1. Allocate result space. */ 
  83.     ret = _PyLong_New(asize + bsize); 
  84.     if (ret == NULL) goto fail; 
  85. #ifdef Py_DEBUG 
  86.     /* Fill with trash, to catch reference to uninitialized digits. */ 
  87.     memset(ret->ob_digit, 0xDF, Py_SIZE(ret) * sizeof(digit)); 
  88. #endif 
  89.  
  90.     /* 2. t1 <- ah*bh, and copy into high digits of result. */ 
  91.     if ((t1 = k_mul(ah, bh)) == NULL) goto fail; 
  92.     assert(Py_SIZE(t1) >= 0); 
  93.     assert(2*shift + Py_SIZE(t1) <= Py_SIZE(ret)); 
  94.     memcpy(ret->ob_digit + 2*shift, t1->ob_digit, 
  95.            Py_SIZE(t1) * sizeof(digit)); 
  96.  
  97.     /* Zero-out the digits higher than the ah*bh copy. */ 
  98.     i = Py_SIZE(ret) - 2*shift - Py_SIZE(t1); 
  99.     if (i) 
  100.         memset(ret->ob_digit + 2*shift + Py_SIZE(t1), 0, 
  101.                i * sizeof(digit)); 
  102.  
  103.     /* 3. t2 <- al*bl, and copy into the low digits. */ 
  104.     if ((t2 = k_mul(al, bl)) == NULL) { 
  105.         Py_DECREF(t1); 
  106.         goto fail; 
  107.     } 
  108.     assert(Py_SIZE(t2) >= 0); 
  109.     assert(Py_SIZE(t2) <= 2*shift); /* no overlap with high digits */ 
  110.     memcpy(ret->ob_digit, t2->ob_digit, Py_SIZE(t2) * sizeof(digit)); 
  111.  
  112.     /* Zero out remaining digits. */ 
  113.     i = 2*shift - Py_SIZE(t2);          /* number of uninitialized digits */ 
  114.     if (i) 
  115.         memset(ret->ob_digit + Py_SIZE(t2), 0, i * sizeof(digit)); 
  116.  
  117.     /* 4 & 5. Subtract ah*bh (t1) and al*bl (t2).  We do al*bl first 
  118.      * because it's fresher in cache. 
  119.      */ 
  120.     i = Py_SIZE(ret) - shift;  /* # digits after shift */ 
  121.     (void)v_isub(ret->ob_digit + shift, i, t2->ob_digit, Py_SIZE(t2)); 
  122.     Py_DECREF(t2); 
  123.  
  124.     (void)v_isub(ret->ob_digit + shift, i, t1->ob_digit, Py_SIZE(t1)); 
  125.     Py_DECREF(t1); 
  126.  
  127.     /* 6. t3 <- (ah+al)(bh+bl), and add into result. */ 
  128.     if ((t1 = x_add(ah, al)) == NULL) goto fail; 
  129.     Py_DECREF(ah); 
  130.     Py_DECREF(al); 
  131.     ah = al = NULL; 
  132.  
  133.     if (a == b) { 
  134.         t2 = t1
  135.         Py_INCREF(t2); 
  136.     } 
  137.     else if ((t2 = x_add(bh, bl)) == NULL) { 
  138.         Py_DECREF(t1); 
  139.         goto fail; 
  140.     } 
  141.     Py_DECREF(bh); 
  142.     Py_DECREF(bl); 
  143.     bh = bl = NULL; 
  144.  
  145.     t3 = k_mul(t1, t2); 
  146.     Py_DECREF(t1); 
  147.     Py_DECREF(t2); 
  148.     if (t3 == NULL) goto fail; 
  149.     assert(Py_SIZE(t3) >= 0); 
  150.  
  151.     /* Add t3.  It's not obvious why we can't run out of room here. 
  152.      * See the (*) comment after this function. 
  153.      */ 
  154.     (void)v_iadd(ret->ob_digit + shift, i, t3->ob_digit, Py_SIZE(t3)); 
  155.     Py_DECREF(t3); 
  156.  
  157.     return long_normalize(ret); 
  158.  
  159.   fail: 
  160.     Py_XDECREF(ret); 
  161.     Py_XDECREF(ah); 
  162.     Py_XDECREF(al); 
  163.     Py_XDECREF(bh); 
  164.     Py_XDECREF(bl); 
  165.     return NULL; 

這里不對 Karatsuba 算法1 的實(shí)現(xiàn)做單獨(dú)解釋,有興趣的朋友可以參考文末的 reference 去了解具體的詳情。

在普通情況下,普通乘法的時(shí)間復(fù)雜度為 n^2 (n 為位數(shù)),而 K 算法的時(shí)間復(fù)雜度為 3n^(log3) ≈ 3n^1.585 ,看起來 K 算法的性能要優(yōu)于普通乘法,那么為什么 Python 不全部使用 K 算法呢?

很簡單,K 算法的優(yōu)勢實(shí)際上要在當(dāng) n 足夠大的時(shí)候,才會對普通乘法形成優(yōu)勢。同時(shí)考慮到內(nèi)存訪問等因素,當(dāng) n 不夠大時(shí),實(shí)際上采用 K 算法的性能將差于直接進(jìn)行乘法。

所以我們來看看 Python 中乘法的實(shí)現(xiàn):

  1. static PyObject * 
  2. long_mul(PyLongObject *a, PyLongObject *b) 
  3.     PyLongObject *z; 
  4.  
  5.     CHECK_BINOP(a, b); 
  6.  
  7.     /* fast path for single-digit multiplication */ 
  8.     if (Py_ABS(Py_SIZE(a)) <= 1 && Py_ABS(Py_SIZE(b)) <= 1) { 
  9.         stwodigits v = (stwodigits)(MEDIUM_VALUE(a)) * MEDIUM_VALUE(b); 
  10.         return PyLong_FromLongLong((long long)v); 
  11.     } 
  12.  
  13.     z = k_mul(a, b); 
  14.     /* Negate if exactly one of the inputs is negative. */ 
  15.     if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) { 
  16.         _PyLong_Negate(&z); 
  17.         if (z == NULL) 
  18.             return NULL; 
  19.     } 
  20.     return (PyObject *)z; 

在這里我們看到,當(dāng)兩個(gè)數(shù)皆小于 2^30-1 時(shí),Python 將直接使用普通乘法并返回,否則將使用 K 算法進(jìn)行計(jì)算

這個(gè)時(shí)候,我們來看一下位運(yùn)算的實(shí)現(xiàn),以右移為例:

  1. static PyObject * 
  2. long_rshift(PyObject *a, PyObject *b) 
  3.     Py_ssize_t wordshift; 
  4.     digit remshift; 
  5.  
  6.     CHECK_BINOP(a, b); 
  7.  
  8.     if (Py_SIZE(b) < 0) { 
  9.         PyErr_SetString(PyExc_ValueError, "negative shift count"); 
  10.         return NULL; 
  11.     } 
  12.     if (Py_SIZE(a) == 0) { 
  13.         return PyLong_FromLong(0); 
  14.     } 
  15.     if (divmod_shift(b, &wordshift, &remshift) < 0
  16.         return NULL; 
  17.     return long_rshift1((PyLongObject *)a, wordshift, remshift); 
  18.  
  19. static PyObject * 
  20. long_rshift1(PyLongObject *a, Py_ssize_t wordshift, digit remshift) 
  21.     PyLongObject *z = NULL
  22.     Py_ssize_t newsize, hishift, i, j; 
  23.     digit lomask, himask; 
  24.  
  25.     if (Py_SIZE(a) < 0) { 
  26.         /* Right shifting negative numbers is harder */ 
  27.         PyLongObject *a1, *a2; 
  28.         a1 = (PyLongObject *) long_invert(a); 
  29.         if (a1 == NULL) 
  30.             return NULL; 
  31.         a2 = (PyLongObject *) long_rshift1(a1, wordshift, remshift); 
  32.         Py_DECREF(a1); 
  33.         if (a2 == NULL) 
  34.             return NULL; 
  35.         z = (PyLongObject *) long_invert(a2); 
  36.         Py_DECREF(a2); 
  37.     } 
  38.     else { 
  39.         newsize = Py_SIZE(a) - wordshift; 
  40.         if (newsize <= 0) 
  41.             return PyLong_FromLong(0); 
  42.         hishift = PyLong_SHIFT - remshift; 
  43.         lomask = ((digit)1 << hishift) - 1; 
  44.         himask = PyLong_MASK ^ lomask; 
  45.         z = _PyLong_New(newsize); 
  46.         if (z == NULL) 
  47.             return NULL; 
  48.         for (i = 0j = wordshift; i < newsize; i++, j++) { 
  49.             z->ob_digit[i] = (a->ob_digit[j] >> remshift) & lomask; 
  50.             if (i+1 < newsize
  51.                 z->ob_digit[i] |= (a->ob_digit[j+1] << hishift) & himask; 
  52.         } 
  53.         z = maybe_small_long(long_normalize(z)); 
  54.     } 
  55.     return (PyObject *)z; 

在這里我們能看到,在兩側(cè)都是小數(shù)的情況下,位移動算法將比普通乘法,存在更多的內(nèi)存分配等操作。這樣也會回答了我們文初所提到的一個(gè)問題,“為什么一些時(shí)候乘法比位運(yùn)算更快”。

總結(jié)本文差不多就到這里了,實(shí)際上通過這次分析我們能得到一些很有趣但是也很冷門的知識。實(shí)際上我們目前看到這樣一個(gè)結(jié)果,是 Python 對于我們常見且高頻的操作所做的一個(gè)特定的設(shè)計(jì)。而這也提醒我們,Python 實(shí)際上對于很多操作都存在自己內(nèi)建的設(shè)計(jì)哲學(xué),在日常使用的時(shí)候,其余語言的經(jīng)驗(yàn),可能無法復(fù)用。

 

責(zé)任編輯:趙寧寧 來源: Manjusaka的編程屋
相關(guān)推薦

2021-01-13 10:51:08

PromissetTimeout(函數(shù)

2023-09-14 15:48:53

排序測試

2023-09-20 00:06:30

Python代碼函數(shù)

2013-07-24 09:47:52

語言語速環(huán)境語言

2012-08-23 09:28:01

編程編程語言

2021-01-30 10:51:07

Python編程語言開發(fā)

2021-04-23 11:11:59

加密貨幣硬幣數(shù)字貨幣

2021-07-23 16:30:36

PythonC++代碼

2020-09-15 09:55:30

類比Python開發(fā)

2014-08-29 09:56:47

排序數(shù)組編程技巧

2015-08-06 12:50:47

技術(shù)人員博客

2024-02-05 22:51:49

AGIRustPython

2022-11-10 15:32:29

2020-02-14 13:53:33

Python 開發(fā)編程語言

2020-07-17 19:31:19

PythonR編程

2019-09-16 12:00:03

constC編程語言

2015-07-31 16:29:15

DockerJavaLinux

2019-04-24 08:00:00

HTTPSHTTP前端

2021-12-27 07:10:26

ClassmethodStaticmetho函數(shù)

2016-12-14 12:02:01

StormHadoop大數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號