Daniel Lemire's blog

Consider using constexpr static function variables for performance in C++

, 2 min read

When programming, we often need constant variables that are used within a single function. For example, you may want to look up characters from a table. The following function is efficient: char table(int idx) { const char array[] = {'z', 'b', 'k', 'd'}; return array[idx]; } It gets trickier…

Science and Technology links (April 11 2023)

, 1 min read

Robert Metcalfe won the Turing Award (the ‘Computer Science Nobel Prize’) for his work on early networking. According to DBLP, Metcalfe published 11 journal articles and 7 peer-review conference papers. Metcalfe was denied his PhD at first. Among other things, he is known for the following…

Programming-language popularity by GitHub pull requests

, 3 min read

GitHub is probably the most popular software repository in the world. One important feature on GitHub is the ‘pull request’: we often contribute to a piece of software by proposing changes to a piece of code. The number of pull requests is not, per se, an objective measure of how much one…

Are your memory-bound benchmarking timings normally distributed?

, 6 min read

When optimizing software, we routinely measure the time that takes a given function or task. Our goal is to decide whether the new code is more or less efficient. The typical assumption is that we get a normal distribution of the timings, and so we should therefore report the average time. If the…

What are we going to do about ChatGPT?

, 5 min read

Generative artificial intelligence, and in particular ChatGPT, has taken the world by storm. Some intellectuals are proposing we create a worldwide ban on advanced AI research, using military operations if needed. Many leading researchers and pundits are proposing a six-month ‘pause’, during…

C++20: consteval and constexpr functions

, 1 min read

Optimizing compilers seek try to push as much of the computation as possible at compile time. In modern C++, you can declare a function as ‘constexpr’, meaning that you state explicitly that the function may be executed at compile time. The constexpr qualifier is not magical. There may not be…

Can GPT pass my programming courses?

, 3 min read

Professeur Caplan reports that ChatGPT, the new artificial intelligence that is all the rage, can pass his economics midterm and get an A. What about computer science ? I submitted to GPT one problem I offer my students… Write a Java program that calculates the sum of numbers from 1 to 10,000…

Counting cycles and instructions on ARM-based Apple systems

, 2 min read

In my blog post Counting cycles and instructions on the Apple M1 processor, I showed how we could have access to “performance counters” to count how many cycles and instructions a given piece of code took on ARM-based mac systems. At the time, we only had access to one Apple processor, the…

Precision, recall and why you shouldn´t crank up the warnings to 11

, 5 min read

Recently, the code hosting site GitHub deployed widely a tool called CodeQL with rather agressive settings. It does static analysis on the code and it attempts to flag problems. I use the phrase “static analysis” to refer to an analysis that does not run the code. Static analysis is limited: it…

Runtime asserts are not free

, 2 min read

When writing software in C and C++, it is common to add C asserts to check that some conditions are satisfied at runtime. Often, it is a simple comparison between two values. In many instances, these asserts are effectively free as far as performance goes, but not always. If they appear within a…

Science and Technology links (March 11 2023)

, 4 min read

In 1500, China was the largest economy in the world, followed by India and France. The USA did not exist yet. In 1700, 4% of human beings lived in France. In the mid 18tg century, there are 25 million inhabitants in France and 5.5 million in England. Yet France had slipped to a sixth place as an…

Trimming spaces from strings faster with SVE on an Amazon Graviton 3 processor

, 2 min read

Programmers sometimes need to trim, or remove, characters, such as spaces from strings. It might be a surprising expensive task. In C/C++, the following function is efficient: size_t trimspaces(const char *s, size_t len, char *out) { char * init_out{out}; for(size_t i = 0; i < len; i++) { …

Float-parsing benchmark: Regular Visual Studio, ClangCL and Linux GCC

, 3 min read

Windows users have choices when it comes to C++ programming. You may choose to stick with the regular Visual Studio. If you prefer, Microsoft makes available ClangCL which couples the LLVM compiler (commonly used by Apple) with the Visual Studio backend. Further, under Windows, you may easily build…

ARM vs Intel on Amazon´s cloud: A URL Parsing Benchmark

, 2 min read

Twitter user opdroid1234 remarked that they are getting more performance out of the ARM nodes than out of the Intel nodes on Amazon’s cloud (AWS). I found previously that the Graviton 3 processors had less bandwidth than comparable Intel systems. However, I have not done much in terms of raw…

Regular Visual Studio versus ClangCL

, 4 min read

If you are programming in C++ using Microsoft tools, you can use the traditional Visual Studio compiler. Or you can use LLVM as a front-end (ClangCL). Let us compare their performance characteristics with a fast string transcoding library (simdutf). I use an up-to-date Visual Studio (2022) with the…

Computing the UTF-8 size of a Latin 1 string quickly (AVX edition)

, 5 min read

Computers represent strings using bytes. Most often, we use the Unicode standard to represent characters in bytes. The universal format to exchange strings online is called UTF-8. It can represent over a million characters while retaining compatibility with the ancient ASCII format. Though most of…

Science and Technology links (February 12 2023)

, 2 min read

Kenny finds that the returns due to education are declining. Rich countries are spending more on education, with comparatively weaker test results. It costs more than ever to train a PhD student, but it takes ever longer for them to complete their studies. There 25 times more crop researchers…

Bit Hacking (with Go code)

, 16 min read

At a fundamental level, a programmer needs to manipulate bits. Modern processors operate over data by loading in ‘registers’ and not individual bits. Thus a programmer must know how to manipulate the bits within a register. Generally, we can do so while programming with 8-bit, 16-bit, 32-bit…