<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 早期綁定速度 > 原文: [http://docs.cython.org/en/latest/src/userguide/early_binding_for_speed.html](http://docs.cython.org/en/latest/src/userguide/early_binding_for_speed.html) 作為一種動態語言,Python 鼓勵一種編程風格,即在方法和屬性方面考慮類和對象,而不是它們適合類層次結構。 這可以使 Python 成為一種非常輕松和舒適的快速開發語言,但需要付出代價 - 管理數據類型的“繁文縟節”會被轉儲到解釋器上。在運行時,解釋器在搜索命名空間,獲取屬性以及解析參數和關鍵字元組方面做了大量工作。與“早期綁定”語言(如 C ++)相比,這種運行時“后期綁定”是 Python 相對緩慢的主要原因。 然而,使用 Cython,通過使用“早期綁定”編程技術可以獲得顯著的加速。 例如,考慮以下(愚蠢)代碼示例: ```py cdef class Rectangle: cdef int x0, y0 cdef int x1, y1 def __init__(self, int x0, int y0, int x1, int y1): self.x0 = x0 self.y0 = y0 self.x1 = x1 self.y1 = y1 def area(self): area = (self.x1 - self.x0) * (self.y1 - self.y0) if area < 0: area = -area return area def rectArea(x0, y0, x1, y1): rect = Rectangle(x0, y0, x1, y1) return rect.area() ``` 在`rectArea()`方法中,對`rect.area()`和`area()`方法的調用包含大量的 Python 開銷。 但是,在 Cython 中,在 Cython 代碼中發生調用的情況下,可以消除大量的開銷。例如: ```py cdef class Rectangle: cdef int x0, y0 cdef int x1, y1 def __init__(self, int x0, int y0, int x1, int y1): self.x0 = x0 self.y0 = y0 self.x1 = x1 self.y1 = y1 cdef int _area(self): area = (self.x1 - self.x0) * (self.y1 - self.y0) if area < 0: area = -area return area def area(self): return self._area() def rectArea(x0, y0, x1, y1): cdef Rectangle rect = Rectangle(x0, y0, x1, y1) return rect._area() ``` 這里,在 Rectangle 擴展類中,我們定義了兩種不同的區域計算方法,即高效的`_area()` C 方法,以及 Python 可調用的`area()`方法,它作為`_area()`的薄包裝器。還要注意函數`rectArea()`我們如何'早期綁定',通過聲明顯式賦予 Rectangle 類型的局部變量`rect`。通過使用此聲明,我們獲得了訪問更有效的 C-callable `_area()`方法的能力,而不僅僅是動態分配給`rect`。 但是 Cython 通過允許我們聲明雙訪問方法 - 可以在 C 級別高效調用的方法,但也可以以 Python 訪問開銷為代價從純 Python 代碼訪問,從而再次為我們提供了更多的簡單性。考慮以下代碼: ```py cdef class Rectangle: cdef int x0, y0 cdef int x1, y1 def __init__(self, int x0, int y0, int x1, int y1): self.x0 = x0 self.y0 = y0 self.x1 = x1 self.y1 = y1 cpdef int area(self): area = (self.x1 - self.x0) * (self.y1 - self.y0) if area < 0: area = -area return area def rectArea(x0, y0, x1, y1): cdef Rectangle rect = Rectangle(x0, y0, x1, y1) return rect.area() ``` 在這里,我們只有一個區域方法,聲明為 [`cpdef`](language_basics.html#cpdef) ,使其可以作為 C 函數有效地調用,但仍然可以從純 Python(或后期綁定的 Cython)代碼訪問。 如果在 Cython 代碼中,我們有一個已經'早期綁定'的變量(即,顯式聲明為 Rectangle 類型,或者轉換為 Rectangle 類型),那么調用其 area 方法將使用高效的 C 代碼路徑并跳過 Python 開銷。但是如果在 Cython 或常規 Python 代碼中我們有一個存儲 Rectangle 對象的常規對象變量,那么調用 area 方法將需要: * 區域方法的屬性查找 * 打包參數的元組和關鍵字的 dict(在這種情況下都是空的) * 使用 Python API 調用該方法 并且在區域方法本身內: * 解析元組和關鍵字 * 執行計算代碼 * 將結果轉換為 python 對象并返回它 因此,在 Cython 中,通過在聲明和轉換變量中使用強類型來實現大規模優化是可能的。對于使用方法調用的緊密循環,以及這些方法是純 C 的情況,差異可能很大。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看