<!DOCTYPE html>
<html lang="vi" data-theme="auto">
<head>

<link rel="preconnect" href="https://www.googletagmanager.com">
<script >(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','GTM-W8MVQXG');</script>
  
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#00add8">
<link rel="canonical" href="https://go.dev/src/math/big/calibrate">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Material+Icons">
<link rel="stylesheet" href="/css/styles.css">
<link rel="icon" href="/images/favicon-gopher.png" sizes="any">
<link rel="apple-touch-icon" href="/images/favicon-gopher-plain.png"/>
<link rel="icon" href="/images/favicon-gopher.svg" type="image/svg+xml">
<link rel="me" href="https://hachyderm.io/@golang">

  
  <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
  new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
  j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
  'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
  })(window,document,'script','dataLayer','GTM-W8MVQXG');</script>
  
<script src="/js/site.js"></script>
<meta name="og:url" content="https://go.dev/src/math/big/calibrate">
<meta name="og:title" content=" - The Go Programming Language">
<title> - The Go Programming Language</title>

<meta name="og:image" content="https://go.dev/doc/gopher/gopher5logo.jpg">
<meta name="twitter:image" content="https://go.dev/doc/gopher/gopherbelly300.jpg">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@golang">
</head>
<body class="Site">
  
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-W8MVQXG"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  


<header class="Site-header js-siteHeader">
  <div class="Header Header--dark">
    <nav class="Header-nav">
      <a href="/">
        <img
          class="js-headerLogo Header-logo"
          src="/images/go-logo-white.svg"
          alt="Go">
      </a>
      <div class="skip-navigation-wrapper">
        <a class="skip-to-content-link" aria-label="Bỏ qua để đến nội dung chính" href="#main-content"> Bỏ qua đến nội dung chính </a>
      </div>
      <div class="Header-rightContent">
        <ul class="Header-menu">
          <li class="Header-menuItem ">
            <a href="#"  class="js-desktop-menu-hover" aria-label=Tại&#32;sao&#32;Go aria-describedby="dropdown-description">
              Tại sao Go <i class="material-icons" aria-hidden="true">arrow_drop_down</i>
            </a>
            <div class="screen-reader-only" id="dropdown-description" hidden>
              Nhấn Enter để bật/tắt menu thả xuống
            </div>
              <ul class="Header-submenu js-desktop-submenu-hover" aria-label="submenu">
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/solutions/case-studies">
                          Case Studies
                          
                        </a>
                    </div>
                    <p>Các vấn đề phổ biến mà doanh nghiệp giải quyết bằng Go</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/solutions/use-cases">
                          Use Cases
                          
                        </a>
                    </div>
                    <p>Câu chuyện về cách thức và lý do các công ty sử dụng Go</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/security/">
                          Bảo mật
                          
                        </a>
                    </div>
                    <p>Go giúp bạn bảo mật theo mặc định như thế nào</p>
                  </li>
              </ul>
          </li>
          <li class="Header-menuItem ">
            <a href="/learn/"  aria-label=Học aria-describedby="dropdown-description">
              Học 
            </a>
            <div class="screen-reader-only" id="dropdown-description" hidden>
              Nhấn Enter để bật/tắt menu thả xuống
            </div>
          </li>
          <li class="Header-menuItem ">
            <a href="#"  class="js-desktop-menu-hover" aria-label=Tài&#32;liệu aria-describedby="dropdown-description">
              Tài liệu <i class="material-icons" aria-hidden="true">arrow_drop_down</i>
            </a>
            <div class="screen-reader-only" id="dropdown-description" hidden>
              Nhấn Enter để bật/tắt menu thả xuống
            </div>
              <ul class="Header-submenu js-desktop-submenu-hover" aria-label="submenu">
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/ref/spec">
                          Go Spec
                          
                        </a>
                    </div>
                    <p>Đặc tả ngôn ngữ Go chính thức</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/doc">
                          Hướng dẫn sử dụng Go
                          
                        </a>
                    </div>
                    <p>Giới thiệu đầy đủ về cách xây dựng phần mềm với Go</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="https://pkg.go.dev/std">
                          Thư viện chuẩn
                          
                        </a>
                    </div>
                    <p>Tài liệu tham chiếu cho thư viện chuẩn của Go</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/doc/devel/release">
                          Ghi chú bản phát hành
                          
                        </a>
                    </div>
                    <p>Tìm hiểu những điểm mới trong từng bản phát hành Go</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/doc/effective_go">
                          Effective Go
                          
                        </a>
                    </div>
                    <p>Mẹo viết code Go rõ ràng, hiệu quả và đúng phong cách</p>
                  </li>
              </ul>
          </li>
          <li class="Header-menuItem ">
            <a href="https://pkg.go.dev"  aria-label=Packages aria-describedby="dropdown-description">
              Packages 
            </a>
            <div class="screen-reader-only" id="dropdown-description" hidden>
              Nhấn Enter để bật/tắt menu thả xuống
            </div>
          </li>
          <li class="Header-menuItem ">
            <a href="#"  class="js-desktop-menu-hover" aria-label=Cộng&#32;đồng aria-describedby="dropdown-description">
              Cộng đồng <i class="material-icons" aria-hidden="true">arrow_drop_down</i>
            </a>
            <div class="screen-reader-only" id="dropdown-description" hidden>
              Nhấn Enter để bật/tắt menu thả xuống
            </div>
              <ul class="Header-submenu js-desktop-submenu-hover" aria-label="submenu">
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/talks/">
                          Recorded Talks
                          
                        </a>
                    </div>
                    <p>Video từ các sự kiện trước đây</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="https://www.meetup.com/pro/go">
                          Meetups
                           <i class="material-icons">open_in_new</i>
                        </a>
                    </div>
                    <p>Gặp gỡ các lập trình viên Go địa phương khác</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/wiki/Conferences">
                          Hội nghị
                           <i class="material-icons">open_in_new</i>
                        </a>
                    </div>
                    <p>Học hỏi và kết nối với các lập trình viên Go trên toàn thế giới</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/blog">
                          Go blog
                          
                        </a>
                    </div>
                    <p>Blog chính thức của dự án Go.</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        <a href="/help">
                          Dự án Go
                          
                        </a>
                    </div>
                    <p>Nhận trợ giúp và cập nhật thông tin từ Go</p>
                  </li>
                  <li class="Header-submenuItem">
                    <div>
                        Kết nối
                    </div>
                    <p></p>
                      <div class="Header-socialIcons">
                        
                        <a class="Header-socialIcon" aria-label="Kết nối qua google-groups (Mở trong cửa sổ mới)" href="https://groups.google.com/g/golang-nuts"><img src="/images/logos/social/google-groups.svg" /></a>
                        <a class="Header-socialIcon" aria-label="Kết nối qua github (Mở trong cửa sổ mới)" href="https://github.com/golang"><img src="/images/logos/social/github.svg" /></a>
                        <a class="Header-socialIcon" aria-label="Kết nối qua bluesky (Mở trong cửa sổ mới)" href="https://bsky.app/profile/golang.org"><img src="/images/logos/social/bluesky.svg" /></a>
                        <a class="Header-socialIcon" aria-label="Kết nối qua mastodon (Mở trong cửa sổ mới)" href="https://hachyderm.io/@golang"><img src="/images/logos/social/mastodon.svg" /></a>
                        <a class="Header-socialIcon" aria-label="Kết nối qua twitter (Mở trong cửa sổ mới)" href="https://twitter.com/golang"><img src="/images/logos/social/twitter.svg" /></a>
                        <a class="Header-socialIcon" aria-label="Kết nối qua reddit (Mở trong cửa sổ mới)" href="https://www.reddit.com/r/golang/"><img src="/images/logos/social/reddit.svg" /></a>
                        <a class="Header-socialIcon" aria-label="Kết nối qua slack (Mở trong cửa sổ mới)" href="https://invite.slack.golangbridge.org/"><img src="/images/logos/social/slack.svg" /></a>
                        <a class="Header-socialIcon" aria-label="Kết nối qua stack-overflow (Mở trong cửa sổ mới)" href="https://stackoverflow.com/tags/go"><img src="/images/logos/social/stack-overflow.svg" /></a>
                      </div>
                  </li>
              </ul>
          </li>
        </ul>
        <button class="Header-navOpen js-headerMenuButton Header-navOpen--white" aria-label="Mở điều hướng.">
        </button>
      </div>
    </nav>
    
  </div>
</header>
<aside class="NavigationDrawer js-header">
  <nav class="NavigationDrawer-nav">
    <div class="NavigationDrawer-header">
      <a href="/">
        <img class="NavigationDrawer-logo" src="/images/go-logo-blue.svg" alt="Go.">
      </a>
    </div>
    <ul class="NavigationDrawer-list">
        
          <li class="NavigationDrawer-listItem js-mobile-subnav-trigger  NavigationDrawer-hasSubnav">
            <a href="#"><span>Tại sao Go</span> <i class="material-icons">navigate_next</i></a>

            <div class="NavigationDrawer NavigationDrawer-submenuItem">
              <nav class="NavigationDrawer-nav">
                <div class="NavigationDrawer-header">
                  <a href="#"><i class="material-icons">navigate_before</i>Tại sao Go</a>
                </div>
                <ul class="NavigationDrawer-list">
                    <li class="NavigationDrawer-listItem">
                        <a href="/solutions/case-studies">
                          Case Studies
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/solutions/use-cases">
                          Use Cases
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/security/">
                          Bảo mật
                          
                        </a>
                      
                    </li>
                </ul>
              </div>
            </div>
          </li>

        
        
          <li class="NavigationDrawer-listItem ">
            <a href="/learn/">Học</a>
          </li>
        
        
          <li class="NavigationDrawer-listItem js-mobile-subnav-trigger  NavigationDrawer-hasSubnav">
            <a href="#"><span>Tài liệu</span> <i class="material-icons">navigate_next</i></a>

            <div class="NavigationDrawer NavigationDrawer-submenuItem">
              <nav class="NavigationDrawer-nav">
                <div class="NavigationDrawer-header">
                  <a href="#"><i class="material-icons">navigate_before</i>Tài liệu</a>
                </div>
                <ul class="NavigationDrawer-list">
                    <li class="NavigationDrawer-listItem">
                        <a href="/ref/spec">
                          Go Spec
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/doc">
                          Hướng dẫn sử dụng Go
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="https://pkg.go.dev/std">
                          Thư viện chuẩn
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/doc/devel/release">
                          Ghi chú bản phát hành
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/doc/effective_go">
                          Effective Go
                          
                        </a>
                      
                    </li>
                </ul>
              </div>
            </div>
          </li>

        
        
          <li class="NavigationDrawer-listItem ">
            <a href="https://pkg.go.dev">Packages</a>
          </li>
        
        
          <li class="NavigationDrawer-listItem js-mobile-subnav-trigger  NavigationDrawer-hasSubnav">
            <a href="#"><span>Cộng đồng</span> <i class="material-icons">navigate_next</i></a>

            <div class="NavigationDrawer NavigationDrawer-submenuItem">
              <nav class="NavigationDrawer-nav">
                <div class="NavigationDrawer-header">
                  <a href="#"><i class="material-icons">navigate_before</i>Cộng đồng</a>
                </div>
                <ul class="NavigationDrawer-list">
                    <li class="NavigationDrawer-listItem">
                        <a href="/talks/">
                          Recorded Talks
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="https://www.meetup.com/pro/go">
                          Meetups
                           <i class="material-icons">open_in_new</i>
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/wiki/Conferences">
                          Hội nghị
                           <i class="material-icons">open_in_new</i>
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/blog">
                          Go blog
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <a href="/help">
                          Dự án Go
                          
                        </a>
                      
                    </li>
                    <li class="NavigationDrawer-listItem">
                        <div>Kết nối</div>
                        <div class="Header-socialIcons">
                          
                            <a class="Header-socialIcon" href="https://groups.google.com/g/golang-nuts"><img src="/images/logos/social/google-groups.svg" /></a>
                            <a class="Header-socialIcon" href="https://github.com/golang"><img src="/images/logos/social/github.svg" /></a>
                            <a class="Header-socialIcon" href="https://bsky.app/profile/golang.org"><img src="/images/logos/social/bluesky.svg" /></a>
                            <a class="Header-socialIcon" href="https://hachyderm.io/@golang"><img src="/images/logos/social/mastodon.svg" /></a>
                            <a class="Header-socialIcon" href="https://twitter.com/golang"><img src="/images/logos/social/twitter.svg" /></a>
                            <a class="Header-socialIcon" href="https://www.reddit.com/r/golang/"><img src="/images/logos/social/reddit.svg" /></a>
                            <a class="Header-socialIcon" href="https://invite.slack.golangbridge.org/"><img src="/images/logos/social/slack.svg" /></a>
                            <a class="Header-socialIcon" href="https://stackoverflow.com/tags/go"><img src="/images/logos/social/stack-overflow.svg" /></a>
                        </div>
                    </li>
                </ul>
              </div>
            </div>
          </li>

        
    </ul>
  </nav>
</aside>
<div class="NavigationDrawer-scrim js-scrim" role="presentation"></div>
<main class="SiteContent SiteContent--default" id="main-content">
  


<article class="Doc Article">










<div id="nav" class="TOC"></div>



<div class='markdown'>
<h1 id="calibration-of-algorithm-thresholds">Calibration of Algorithm Thresholds</h1>
<p>This document describes the approach to calibration of algorithmic thresholds in
<code>math/big</code>, implemented in <a href="calibrate_test.go">calibrate_test.go</a>.</p>
<p>Basic operations like multiplication and division have many possible implementations.
Most algorithms that are better asymptotically have overheads that make them
run slower for small inputs. When presented with an operation to run, <code>math/big</code>
must decide which algorithm to use.</p>
<p>For example, for small inputs, multiplication using the “grade school algorithm” is fastest.
Given multi-digit x, y and a target z: clear z, and then for each digit y[i], z[i:] += x*y[i].
That last operation, adding a vector times a digit to another vector (including carrying up
the vector during the multiplication and addition), can be implemented in a tight assembly loop.
The overall speed is O(N**2) where N is the number of digits in x and y (assume they match),
but the tight inner loop performs well for small inputs.</p>
<p><a href="https://en.wikipedia.org/wiki/Karatsuba_algorithm" rel="noreferrer" target="_blank">Karatsuba&rsquo;s algorithm</a>
multiplies two N-digit numbers by splitting them in half, computing
three N/2-digit products, and then reconstructing the final product using a few more
additions and subtractions. It runs in O(N**log₂ 3) = O(N**1.58) time.
The grade school loop runs faster for small inputs,
but eventually Karatsuba&rsquo;s smaller asymptotic run time wins.</p>
<p>The multiplication implementation must decide which to use.
Under the assumption that once Karatsuba is faster for some N,
it will be larger for all larger N as well,
the rule is to use Karatsuba&rsquo;s algorithm when the input length N ≥ karatsubaThreshold.</p>
<p>Calibration is the process of determining what karatsubaThreshold should be set to.
It doesn&rsquo;t sound like it should be that hard, but it is:</p>
<ul>
<li>Theoretical analysis does not help: the answer depends on the actual machines
and the actual constant factors in the two implementations.</li>
<li>We are picking a single karatsubaThreshold for all systems,
despite them having different relative execution speeds for the operations
in the two algorithms.
(We could in theory pick different thresholds for different architectures,
but there can still be significant variation within a given architecture.)</li>
<li>The assumption that there is a single N where
an asymptotically better algorithm becomes faster and stays faster
is not true in general.</li>
<li>Recursive algorithms like Karatsuba&rsquo;s may have  different optimal
thresholds for different large input sizes.</li>
<li>Thresholds can interfere. For example, changing the karatsubaThreshold makes
multiplication faster or slower, which in turn affects the best divRecursiveThreshold
(because divisions use multiplication).</li>
</ul>
<p>The best we can do is measure the performance of the overall multiplication
algorithm across a variety of inputs and thresholds and look for a threshold
that balances all these concerns reasonably well,
setting thresholds in dependency order (for example, multiplication before division).</p>
<p>The code in <code>calibrate_test.go</code> does this measurement of a variety of input sizes
and threshold values and prints the timing results as a CSV file.
The code in <code>calibrate_graph.go</code> reads the CSV and writes out an SVG file plotting the data.
For example:</p>
<pre><code>go test -run=Calibrate/KaratsubaMul -timeout=1h -calibrate &gt;kmul.csv
go run calibrate_graph.go kmul.csv &gt;kmul.svg
</code></pre>
<p>Any particular input is sensitive to only a few transitions in threshold.
For example, an input of size 320 recurses on inputs of size 160,
which recurses on inputs of size 80,
which recurses on inputs of size 40,
and so on, until falling below the Karatsuba threshold.
Here is what the timing looks like for an input of size 320,
normalized so that 1.0 is the fastest timing observed:</p>
<p><img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.mac320.svg" alt="KaratsubaThreshold on an Apple M3 Pro, N=320 only"></p>
<p>For this input, all thresholds from 21 to 40 perform optimally and identically: they all mean “recurse at N=40 but not at N=20”.
From the single input of size N=320, we cannot decide which of these 20 thresholds is best.</p>
<p>Other inputs exercise other decision points. For example, here is the timing for N=240:</p>
<p><img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.mac240.svg" alt="KaratsubaThreshold on an Apple M3 Pro, N=240 only"></p>
<p>In this case, all the thresholds from 31 to 60 perform optimally and identically, recursing at N=60 but not N=30.</p>
<p>If we combine these two into a single graph and then plot the geometric mean of the two lines in blue,
the optimal range becomes a little clearer:</p>
<p><img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.mac240+320.svg" alt="KaratsubaThreshold on an Apple M3 Pro"></p>
<p>The actual calibration runs all possible inputs from size N=200 to N=400, in increments of 8,
plotting all 26 lines in a faded gray (note the changed y-axis scale, zooming in near 1.0).</p>
<p><img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.mac.svg" alt="KaratsubaThreshold on an Apple M3 Pro"></p>
<p>Now the optimal value is clear: the best threshold on this chip, with these algorithmic implementations, is 40.</p>
<p>Unfortunately, other chips are different. Here is an Intel Xeon server chip:</p>
<p><img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.c2s16.svg" alt="KaratsubaThreshold on an Apple M3 Pro"></p>
<p>On this chip, the best threshold is closer to 60. Luckily, 40 is not a terrible choice either: it is only about 2% slower on average.</p>
<p>The rest of this document presents the timings measured for the <code>math/big</code> thresholds on a variety of machines
and justifies the final thresholds. The timings used these machines:</p>
<ul>
<li>The <code>gotip-linux-amd64_c3h88-perf_vs_release</code> gomote, a Google Cloud c3-high-88 machine using an Intel Xeon Platinum 8481C CPU (Emerald Rapids).</li>
<li>The <code>gotip-linux-amd64_c2s16-perf_vs_release</code> gomote, a Google Cloud c2-standard-16 machine using an Intel Xeon Gold 6253CL CPU (Cascade Lake).</li>
<li>A home server built with an AMD Ryzen 9 7950X CPU.</li>
<li>The <code>gotip-linux-arm64_c4as16-perf_vs_release</code> gomote, a Google Cloud c4a-standard-16 machine using Google&rsquo;s Axiom Arm CPU.</li>
<li>An Apple MacBook Pro with an Apple M3 Pro CPU.</li>
</ul>
<p>In general, we break ties in favor of the newer c3h88 x86 perf gomote, then the c4as16 arm64 perf gomote, and then the others.</p>
<h2 id="karatsuba-multiplication">Karatsuba Multiplication</h2>
<p>Here are the full results for the Karatsuba multiplication threshold.</p>
<p><img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.c3h88.svg" alt="KaratsubaThreshold on an Intel Xeon Platium 8481C">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.c2s16.svg" alt="KaratsubaThreshold on an Intel Xeon Gold 6253CL">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.s7.svg" alt="KaratsubaThreshold on an AMD Ryzen 9 7950X">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.c4as16.svg" alt="KaratsubaThreshold on an Axiom Arm">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaMul/cal.mac.svg" alt="KaratsubaThreshold on an Apple M3 Pro"></p>
<p>The majority of systems have optimum thresholds near 40, so we chose karatsubaThreshold = 40.</p>
<h2 id="basic-squaring">Basic Squaring</h2>
<p>For squaring a number (<code>z.Mul(x, x)</code>), math/big uses grade school multiplication
up to basicSqrThreshold, where it switches to a customized algorithm that is
still quadratic but avoids half the word-by-word multiplies
since the two arguments are identical.
That algorithm&rsquo;s inner loops are not as tight as the grade school multiplication,
so it is slower for small inputs. How small?</p>
<p>Here are the timings:</p>
<p><img src="https://swtch.com/math/big/_calibrate/BasicSqr/cal.c3h88.svg" alt="BasicSqrThreshold on an Intel Xeon Platium 8481C">
<img src="https://swtch.com/math/big/_calibrate/BasicSqr/cal.c2s16.svg" alt="BasicSqrThreshold on an Intel Xeon Gold 6253CL">
<img src="https://swtch.com/math/big/_calibrate/BasicSqr/cal.s7.svg" alt="BasicSqrThreshold on an AMD Ryzen 9 7950X">
<img src="https://swtch.com/math/big/_calibrate/BasicSqr/cal.c4as16.svg" alt="BasicSqrThreshold on an Axiom Arm">
<img src="https://swtch.com/math/big/_calibrate/BasicSqr/cal.mac.svg" alt="BasicSqrThreshold on an Apple M3 Pro"></p>
<p>These inputs are so small that the calibration times batches of 100 instead of individual operations.
There is no one best threshold, even on a single system, because some of the sizes seem to run
the grade school algorithm faster than others.
For example, on the AMD CPU,
for N=14, basic squaring is 4% faster than basic multiplication,
suggesting the threshold has been crossed,
but for N=16, basic multiplication is 9% faster than basic squaring,
probably because the tight assembly can use larger chunks.</p>
<p>It is unclear why the Axiom Arm timings are so incredibly noisy.</p>
<p>We chose basicSqrThreshold = 12.</p>
<h2 id="karatsuba-squaring">Karatsuba Squaring</h2>
<p>Beyond the basic squaring threshold, at some point a customized Karatsuba can take over.
It uses three half-sized squarings instead of three half-sized multiplies.
Here are the timings:</p>
<p><img src="https://swtch.com/math/big/_calibrate/KaratsubaSqr/cal.c3h88.svg" alt="KaratsubaSqrThreshold on an Intel Xeon Platium 8481C">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaSqr/cal.c2s16.svg" alt="KaratsubaSqrThreshold on an Intel Xeon Gold 6253CL">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaSqr/cal.s7.svg" alt="KaratsubaSqrThreshold on an AMD Ryzen 9 7950X">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaSqr/cal.c4as16.svg" alt="KaratsubaSqrThreshold on an Axiom Arm">
<img src="https://swtch.com/math/big/_calibrate/KaratsubaSqr/cal.mac.svg" alt="KaratsubaSqrThreshold on an Apple M3 Pro"></p>
<p>The majority of chips preferred a lower threshold, around 60-70,
but the older Intel Xeon and the AMD prefer a threshold around 100-120.</p>
<p>We chose karatsubaSqrThreshold = 80, which is within 2% of optimal on all the chips.</p>
<h2 id="recursive-division">Recursive Division</h2>
<p>Division uses a recursive divide-and-conquer algorithm for large inputs,
eventually falling back to a more traditional grade-school whole-input trial-and-error division.
Here are the timings for the threshold between the two:</p>
<p><img src="https://swtch.com/math/big/_calibrate/DivRecursive/cal.c3h88.svg" alt="DivRecursiveThreshold on an Intel Xeon Platium 8481C">
<img src="https://swtch.com/math/big/_calibrate/DivRecursive/cal.c2s16.svg" alt="DivRecursiveThreshold on an Intel Xeon Gold 6253CL">
<img src="https://swtch.com/math/big/_calibrate/DivRecursive/cal.s7.svg" alt="DivRecursiveThreshold on an AMD Ryzen 9 7950X">
<img src="https://swtch.com/math/big/_calibrate/DivRecursive/cal.c4as16.svg" alt="DivRecursiveThreshold on an Axiom Arm">
<img src="https://swtch.com/math/big/_calibrate/DivRecursive/cal.mac.svg" alt="DivRecursiveThreshold on an Apple M3 Pro"></p>
<p>We chose divRecursiveThreshold = 40.</p>
</div>







</article>



</main>
<footer class="Site-footer">
  <div class="Footer">
    <div class="Container">
      <div class="Footer-links">
          <div class="Footer-linkColumn">
            <a href="/solutions/" class="Footer-link Footer-link--primary" aria-describedby="footer-description">
              Tại sao Go
            </a>
              <a href="/solutions/use-cases" class="Footer-link" aria-describedby="footer-description">
                Use Cases
              </a>
              <a href="/solutions/case-studies" class="Footer-link" aria-describedby="footer-description">
                Case Studies
              </a>
          </div>
          <div class="Footer-linkColumn">
            <a href="/learn/" class="Footer-link Footer-link--primary" aria-describedby="footer-description">
              Bắt đầu
            </a>
              <a href="/play" class="Footer-link" aria-describedby="footer-description">
                Playground
              </a>
              <a href="/tour/" class="Footer-link" aria-describedby="footer-description">
                Tour
              </a>
              <a href="https://stackoverflow.com/questions/tagged/go?tab=Newest" class="Footer-link" aria-describedby="footer-description">
                Stack Overflow
              </a>
              <a href="/help/" class="Footer-link" aria-describedby="footer-description">
                Trợ giúp
              </a>
          </div>
          <div class="Footer-linkColumn">
            <a href="https://pkg.go.dev" class="Footer-link Footer-link--primary" aria-describedby="footer-description">
              Packages
            </a>
              <a href="/pkg/" class="Footer-link" aria-describedby="footer-description">
                Thư viện chuẩn
              </a>
              <a href="https://pkg.go.dev/about" class="Footer-link" aria-describedby="footer-description">
                Giới thiệu về Go Packages
              </a>
          </div>
          <div class="Footer-linkColumn">
            <a href="/project" class="Footer-link Footer-link--primary" aria-describedby="footer-description">
              Giới thiệu
            </a>
              <a href="/dl/" class="Footer-link" aria-describedby="footer-description">
                Tải xuống
              </a>
              <a href="/blog/" class="Footer-link" aria-describedby="footer-description">
                Blog
              </a>
              <a href="https://github.com/golang/go/issues" class="Footer-link" aria-describedby="footer-description">
                Issue Tracker
              </a>
              <a href="/doc/devel/release" class="Footer-link" aria-describedby="footer-description">
                Ghi chú bản phát hành
              </a>
              <a href="/brand" class="Footer-link" aria-describedby="footer-description">
                Hướng dẫn thương hiệu
              </a>
              <a href="/conduct" class="Footer-link" aria-describedby="footer-description">
                Quy tắc ứng xử
              </a>
          </div>
          <div class="Footer-linkColumn">
            <a href="/wiki/#the-go-community" class="Footer-link Footer-link--primary" aria-describedby="footer-description">
              Kết nối
            </a>
              <a href="https://bsky.app/profile/golang.org" class="Footer-link" aria-describedby="footer-description">
                Bluesky
              </a>
              <a href="https://hachyderm.io/@golang" class="Footer-link" aria-describedby="footer-description">
                Mastodon
              </a>
              <a href="https://www.twitter.com/golang" class="Footer-link" aria-describedby="footer-description">
                Twitter
              </a>
              <a href="https://github.com/golang" class="Footer-link" aria-describedby="footer-description">
                GitHub
              </a>
              <a href="https://invite.slack.golangbridge.org/" class="Footer-link" aria-describedby="footer-description">
                Slack
              </a>
              <a href="https://reddit.com/r/golang" class="Footer-link" aria-describedby="footer-description">
                r/golang
              </a>
              <a href="https://www.meetup.com/pro/go" class="Footer-link" aria-describedby="footer-description">
                Meetup
              </a>
              <a href="https://golangweekly.com/" class="Footer-link" aria-describedby="footer-description">
                Golang Weekly
              </a>
          </div>
      </div>
    </div>
  </div>
  <div class="screen-reader-only" id="footer-description" hidden>
          Mở trong cửa sổ mới.
  </div>
  <div class="Footer">
    <div class="Container Container--fullBleed">
      <div class="Footer-bottom">
        <img class="Footer-gopher" src="/images/gophers/pilot-bust.svg" alt="Go Gopher">
        <ul class="Footer-listRow">
          <li class="Footer-listItem">
            <a href="/copyright" aria-describedby="footer-description">Bản quyền</a>
          </li>
          <li class="Footer-listItem">
            <a href="/tos" aria-describedby="footer-description">Điều khoản dịch vụ</a>
          </li>
          <li class="Footer-listItem">
            <a href="http://www.google.com/intl/en/policies/privacy/" aria-describedby="footer-description"
              target="_blank"
              rel="noopener">
              Chính sách quyền riêng tư
            </a>
            </li>
          <li class="Footer-listItem">
            <a
              href="/s/website-issue" aria-describedby="footer-description"
              target="_blank"
              rel="noopener"
              >
              Báo cáo sự cố
            </a>
          </li>
          <li class="Footer-listItem go-Footer-listItem">
            <button class="go-Button go-Button--text go-Footer-toggleTheme js-toggleTheme" aria-label="Chuyển đổi giao diện">
              <img
                data-value="auto"
                class="go-Icon go-Icon--inverted"
                height="24"
                width="24"
                src="/images/icons/brightness_6_gm_grey_24dp.svg"
                alt="Giao diện hệ thống">
              <img
                data-value="dark"
                class="go-Icon go-Icon--inverted"
                height="24"
                width="24"
                src="/images/icons/brightness_2_gm_grey_24dp.svg"
                alt="Giao diện tối">
              <img
                data-value="light"
                class="go-Icon go-Icon--inverted"
                height="24"
                width="24"
                src="/images/icons/light_mode_gm_grey_24dp.svg"
                alt="Giao diện sáng">
            </button>
          </li>
        </ul>
        <a class="Footer-googleLogo" target="_blank" href="https://google.com" rel="noopener">
          <img class="Footer-googleLogoImg" src="/images/google-white.png" alt="Logo Google">
        </a>
      </div>
    </div>
  </div>
  <script src="/js/jquery.js"></script>
  <script src="/js/carousels.js"></script>
  <script src="/js/searchBox.js"></script>
  <script src="/js/misc.js"></script>
  <script src="/js/hats.js"></script>
  <script src="/js/playground.js"></script>
  <script src="/js/godocs.js"></script>
  <script async src="/js/copypaste.js"></script>
</footer>
<section class="Cookie-notice js-cookieNotice">
  <div>go.dev sử dụng cookie của Google để cung cấp và nâng cao chất lượng dịch vụ cũng như
  phân tích lưu lượng truy cập. <a target=_blank href="https://policies.google.com/technologies/cookies">Tìm hiểu thêm.</a></div>
  <div><button class="go-Button">Đồng ý</button></div>
</section>
</body>
</html>


















