<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Python⇒Speed</title>
 <link href="https://pythonspeed.com/atom.xml" rel="self"/>
 <link href="https://pythonspeed.com/"/>
 <updated>2026-08-02T21:15:50+00:00</updated>
 <id>https://pythonspeed.com/</id>
 <author>
   <name>Itamar Turner-Trauring</name>
   <email>itamar@pythonspeed.com</email>
 </author>

 
 
 <entry>
   <title>Faster floating point math with Rust’s new API</title>
   <link href="https://pythonspeed.com/articles/faster-float-math-rust/"/>
   <updated>2026-08-02T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/faster-float-math-rust</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/faster-float-math-rust/">&lt;p&gt;Floating point math is often slower than integer math because the
compiler is being conservative about how it optimizes your code. While
some programming languages already had solutions of a sort, until now
Rust did not have a good stable way to deal with this limitation. But
now, starting in version 1.98, Rust will allow telling the compiler it
can optimize your code further—but with extra control so that you can
still write numeric algorithms with minimal rounding errors.&lt;/p&gt;

&lt;p&gt;In this article you will learn:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Why by default the compiler won’t optimize floating point math as much
as it does integer math.&lt;/li&gt;
  &lt;li&gt;Rust’s new API to solve this limitation.&lt;/li&gt;
  &lt;li&gt;Examples of using this new API, its speed impact, and how you can
control where it is used.&lt;/li&gt;
&lt;/ul&gt;


   &lt;a href="https://pythonspeed.com/articles/faster-float-math-rust/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>8× faster binary search: from compiled code to mechanical sympathy</title>
   <link href="https://pythonspeed.com/articles/branchless-binary-search/"/>
   <updated>2026-07-11T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/branchless-binary-search</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/branchless-binary-search/">&lt;p&gt;How do you speed up computational Python code? A common, and useful,
starting point is:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Pick a good algorithm.&lt;/li&gt;
  &lt;li&gt;Use a compiled language to write a Python extension.&lt;/li&gt;
  &lt;li&gt;Maybe add parallelism so you can use multiple CPU cores.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But what if you need more speed? Consider the following real problem,
one of the steps in scikit-learn’s gradient histogram boosting
algorithm:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;You have a large array of floating point numbers.&lt;/li&gt;
  &lt;li&gt;You want to assign them to the integer range 0-254, spread out evenly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;scikit-learn implements this by splitting up the full range of float
values into 255 buckets, creating a sorted array of bucket boundaries,
and then using binary search to choose the appropriate bucket for each
value. The binary search is implemented in a compiled language, and it
can run in parallel on multiple cores.&lt;/p&gt;

&lt;p&gt;Recently, as part of my work at
&lt;a href=&quot;https://labs.quansight.org/&quot;&gt;Quansight&lt;/a&gt;, and inspired by
&lt;a href=&quot;https://pvk.ca/Blog/2012/07/03/binary-search-star-eliminates-star-branch-mispredictions/&quot;&gt;two&lt;/a&gt;
&lt;a href=&quot;https://pvk.ca/Blog/2015/11/29/retrospective-on-binary-search-and-on-compression-slash-compilation/&quot;&gt;posts&lt;/a&gt;
by Paul Khuong, I sped up this implementation significantly. How? By
making sure the code wasn’t fighting against the CPU.&lt;/p&gt;

&lt;p&gt;In this article I’m going to walk you through that speed-up,
demonstrated on a simplified example. Then I’m going to demonstrate a
series of additional optimizations, with the final version running 6×
faster than the original one.&lt;/p&gt;

&lt;p&gt;It’s worth knowing that I will be speeding through mentions of many
different low-level hardware topics: instruction-level parallelism,
branch (mis)prediction, and more. This is only one
article, it can only briefly introduce you to what’s possible, it can’t
function as an in-depth tutorial. So I’ll talk about how you can learn
more about these topics at the end of the article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updates:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Fixed stupid mistake involving f64 comparisons, you can just do them.
SIMD doesn’t help anymore—but that’s OK, the code is even faster.&lt;/li&gt;
  &lt;li&gt;Clarified benefits of precalculating &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;halves&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


   &lt;a href="https://pythonspeed.com/articles/branchless-binary-search/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>Timesliced reservoir sampling: a new(?) algorithm for profilers</title>
   <link href="https://pythonspeed.com/articles/reservoir-sampling-profilers/"/>
   <updated>2026-04-01T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/reservoir-sampling-profilers</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/reservoir-sampling-profilers/">&lt;p&gt;Imagine you are processing a stream of events, of unknown length. It
could end in 3 seconds, it could run for 3 months; you simply don’t
know. As a result, storing the whole stream in memory or even on disk is
not acceptable, but you still need to extract relevant information.&lt;/p&gt;

&lt;p&gt;Depending on what information you need, choosing a random sample of the
stream will give you almost as good information as storing all the
data. For example, consider a performance profiler, used to find which
parts of your running code are slowest. Many profilers records a
program’s callstack every few microseconds, resulting a stream of
unlimited size: you don’t know how long the program will run. For this
use case, a random sample of callstacks, say 2000 of them, can usually
give you sufficient information to do performance optimization.&lt;/p&gt;

&lt;p&gt;Why does this work?&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Slow code will result in the same callstack being repeated.&lt;/li&gt;
  &lt;li&gt;A random sample of callstacks is more likely to contain callstacks
that repeat a lot.&lt;/li&gt;
  &lt;li&gt;Thus, a random sample is more likely to include slow code, the code
you specifically want to identify with your profiler.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you need to extract a random sample from a stream of unknown
length, a common solution is the family of algorithms known as
&lt;a href=&quot;https://en.wikipedia.org/wiki/Reservoir_sampling&quot;&gt;reservoir sampling&lt;/a&gt;.
In this article you will learn:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;How basic reservoir sampling works.&lt;/li&gt;
  &lt;li&gt;Some problems with reservoir sampling, motivated by a profiler that
wants to generate a timeline.&lt;/li&gt;
  &lt;li&gt;A (new?) variant of reservoir sampling that allows you to ensure
samples are spread evenly across time.&lt;/li&gt;
&lt;/ul&gt;


   &lt;a href="https://pythonspeed.com/articles/reservoir-sampling-profilers/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>Unit testing your code's performance, part 2: Catching speed changes</title>
   <link href="https://pythonspeed.com/articles/speed-unit-tests/"/>
   <updated>2026-02-24T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/speed-unit-tests</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/speed-unit-tests/">&lt;p&gt;In a previous post I talked about unit testing for speed, and in particular &lt;a href=&quot;/articles/big-o-tests/&quot;&gt;testing for big-O scalability&lt;/a&gt;.
The next step is catching cases where you’ve changed not the scalability, but the direct efficiency of your code.&lt;/p&gt;

&lt;p&gt;If your first thought is “how this is different from running benchmarks?”, well, good point!
An excellent starting point for performance is implementing a benchmark that runs automatically in CI, on every single pull request.
If you haven’t got that, you probably want to go do that first.&lt;/p&gt;

&lt;p&gt;Once you have implemented CI benchmarks, they will typically run when you submit a pull request or the equivalent.
And if you’re doing performance work, that’s hopefully just a formality, as you likely have been benchmarking your code locally as you work.&lt;/p&gt;

&lt;p&gt;But what happens when you or a colleague are working on features or bugfixes, and accidentally modify a performance-critical code path?
You make changes, run the tests locally, run a linter, open a pull request… and &lt;em&gt;now&lt;/em&gt; the benchmark runs, and tells you that your code has made things slower.
This is annoying, because now you have to go back and figure out which specific change was the cause.&lt;/p&gt;

&lt;p&gt;So what you really want is to get some sense of whether performance changed much earlier in the process, giving you immediate feedback when you’re running tests locally.
Since a reliable benchmark environment is hard, switching to a test might allow for an early warning.&lt;/p&gt;


   &lt;a href="https://pythonspeed.com/articles/speed-unit-tests/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>The best Docker base image for your Python application (February 2026)</title>
   <link href="https://pythonspeed.com/articles/base-image-python-docker-images/"/>
   <updated>2026-01-30T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/base-image-python-docker-images</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/base-image-python-docker-images/">&lt;p&gt;When you’re building a Docker image for your Python application, you’re building on top of an existing image—and there are many possible choices for the resulting container.
There are OS images like Ubuntu, and there are the many different variants of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;python&lt;/code&gt; base image.
And now there’s a new choice, installing Python using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;uv&lt;/code&gt;, which allows you to use any base image you’d like.&lt;/p&gt;

&lt;p&gt;Which one should you use?
Which one is better?
There are many choices, and it may not be obvious which is the best for your situation.&lt;/p&gt;

&lt;p&gt;So to help you make a choice that fits your needs, in this article I’ll go through some of the relevant criteria, and suggest some reasonable defaults that will work for most people.&lt;/p&gt;


   &lt;a href="https://pythonspeed.com/articles/base-image-python-docker-images/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>Speeding up NumPy with parallelism</title>
   <link href="https://pythonspeed.com/articles/numpy-parallelism/"/>
   <updated>2026-01-29T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/numpy-parallelism</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/numpy-parallelism/">&lt;p&gt;If your NumPy code is too slow, what next?&lt;/p&gt;

&lt;p&gt;One option is taking advantage of the multiple cores on your CPU: using
a thread pool to do work in parallel. Another option is to tune your
code so it’s less wasteful. Or, since these are two different sources of
speed, you can do both.&lt;/p&gt;

&lt;p&gt;In this article I’ll cover:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A simple example of making a NumPy algorithm parallel.&lt;/li&gt;
  &lt;li&gt;A separate kind of optimization, making a more efficient
implementation in Numba.&lt;/li&gt;
  &lt;li&gt;How to get even more speed by using both at once.&lt;/li&gt;
  &lt;li&gt;Aside: A hardware limit on parallelism.&lt;/li&gt;
  &lt;li&gt;Aside: Why not Numba’s built-in parallelism?&lt;/li&gt;
&lt;/ul&gt;


   &lt;a href="https://pythonspeed.com/articles/numpy-parallelism/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>Unit testing your code's performance, part 1: Big-O scaling</title>
   <link href="https://pythonspeed.com/articles/big-o-tests/"/>
   <updated>2026-01-07T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/big-o-tests</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/big-o-tests/">&lt;p&gt;When you implement an algorithm, you also implement tests to make sure the outputs are correct.
This can help you:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Ensure your code is correct.&lt;/li&gt;
  &lt;li&gt;Catch problems if and when you change it in the future.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re trying to make sure your software is fast, or at least doesn’t get slower, automated tests for performance would also be useful.
But where should you start?&lt;/p&gt;

&lt;p&gt;My suggestion: start by testing big-O scaling.
It’s a critical aspect of your software’s speed, and it doesn’t require a complex benchmarking setup.
In this article I’ll cover:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A reminder of what big-O scaling means for algorithms.&lt;/li&gt;
  &lt;li&gt;Why this is such a critical performance property.&lt;/li&gt;
  &lt;li&gt;Identifying your algorithm’s scalability, including empirically with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bigO&lt;/code&gt; library.&lt;/li&gt;
  &lt;li&gt;Using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bigO&lt;/code&gt; library to test your Python code’s big-O scalability.&lt;/li&gt;
&lt;/ul&gt;


   &lt;a href="https://pythonspeed.com/articles/big-o-tests/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>Testing the compiler optimizations your code relies on</title>
   <link href="https://pythonspeed.com/articles/testing-compiler-optimizations/"/>
   <updated>2025-09-09T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/testing-compiler-optimizations</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/testing-compiler-optimizations/">&lt;p&gt;In a &lt;a href=&quot;https://davidlattimore.github.io/posts/2025/09/02/rustforge-wild-performance-tricks.html&quot;&gt;recent article by David
Lattimore&lt;/a&gt;,
he demonstrates a number of Rust performance tricks, including one that involve writing code that
looks like a loop, but which in practice is optimized down to a fixed
number of instructions. Having what looks like an &lt;em&gt;O&lt;/em&gt;(&lt;em&gt;n&lt;/em&gt;) loop turned
into a constant operation is great for speed!&lt;/p&gt;

&lt;p&gt;But there’s a problem with this sort of trick: how do you know the
compiler will &lt;em&gt;keep&lt;/em&gt; doing it? What happens when the compiler’s next
release comes out? How can you catch performance regressions?&lt;/p&gt;

&lt;p&gt;One solution is benchmarking: you measure your code’s speed, and if it
gets a lot slower, something has gone wrong. This is useful and
important if you care about speed. But it’s also less localized, so it
won’t necessarily immediately pinpoint where the regression happened.&lt;/p&gt;

&lt;p&gt;In this article I’m going to cover another approach: a test that will
only pass if the compiler really did optimize the loop away.&lt;/p&gt;


   &lt;a href="https://pythonspeed.com/articles/testing-compiler-optimizations/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>330× faster: Four different ways to speed up your code</title>
   <link href="https://pythonspeed.com/articles/different-ways-speed/"/>
   <updated>2025-07-02T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/different-ways-speed</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/different-ways-speed/">&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The original version of this article was slightly different, e.g. with 500x speedup; I reworked it to make the argument clearer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If your Python code is slow and needs to be &lt;em&gt;fast&lt;/em&gt;, there are many
different approaches you can take, from parallelism to writing a
compiled extension. But if you just stick to one approach, it’s easy to
miss potential speedups, and end up with code that is much slower than
it could be.&lt;/p&gt;

&lt;p&gt;To make sure you’re not forgetting potential sources of speed, it’s
useful to think in terms of &lt;em&gt;practices&lt;/em&gt;. Each practice:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Speeds up your code in its own unique way.&lt;/li&gt;
  &lt;li&gt;Involves distinct skills and knowledge.&lt;/li&gt;
  &lt;li&gt;Can be applied on its own.&lt;/li&gt;
  &lt;li&gt;Can also be applied together with other practices for even more
speed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To make this more concrete, in this article I’ll work through an example
where I will apply multiple practices. Specifically I’ll be
demonstrating the practices of:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Efficiency:&lt;/strong&gt; Getting rid of wasteful or repetitive calculations.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Compilation:&lt;/strong&gt; Using a compiled language, and potentially working around the
compiler’s limitations.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Parallelism:&lt;/strong&gt; Using multiple CPU cores.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Process:&lt;/strong&gt; Using development processes that result in faster code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We’ll see that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Applying just the Practice of Efficiency to this problem gave me an
almost 2× speed-up.&lt;/li&gt;
  &lt;li&gt;Applying just the Practice of Compilation gave me a 10× speed-up.&lt;/li&gt;
  &lt;li&gt;When I applied both, the result was even faster.&lt;/li&gt;
  &lt;li&gt;Following up with the Practice of Parallelism gave even more of a
speedup, for a final speed up of 330×.&lt;/li&gt;
&lt;/ul&gt;


   &lt;a href="https://pythonspeed.com/articles/different-ways-speed/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 
 
 <entry>
   <title>Loading Pydantic models from JSON without running out of memory</title>
   <link href="https://pythonspeed.com/articles/pydantic-json-memory/"/>
   <updated>2025-05-22T00:00:00+00:00</updated>
   <id>https://pythonspeed.com/articles/pydantic-json-memory</id>
   <content type="html" xml:base="https://pythonspeed.com/articles/pydantic-json-memory/">&lt;p&gt;You have a large JSON file, and you want to load the data into Pydantic.
Unfortunately, this uses a lot of memory, to the point where large JSON files are very difficult to read.
What to do?&lt;/p&gt;

&lt;p&gt;Assuming you’re stuck with JSON, in this article we’ll cover:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The high memory usage you get with Pydantic’s default JSON loading.&lt;/li&gt;
  &lt;li&gt;How to reduce memory usage by switching to another JSON library.&lt;/li&gt;
  &lt;li&gt;Going further by switching to dataclasses with slots.&lt;/li&gt;
&lt;/ul&gt;


   &lt;a href="https://pythonspeed.com/articles/pydantic-json-memory/"&gt;Read more...&lt;/a&gt;</content>
 </entry>
 
 

</feed>
