Daniel Lemire's blog

How smart is Swift with abstraction? A trivial experiment with protocols

, 1 min read

Apple’s Swift programming language has the notion of “protocol” which is similar to an interface in Java or Go. So we can define a protocol that has a single function. public protocol Getter { func get(_ index : Int) -> Int } We need to define at least one class that has the prescribed…

Science and Technology links (September 29th, 2017)

, 4 min read

Elon Musk presented his plans for space exploration. It is pretty close to science fiction (right out of Star Trek) with the exception that Musk has a track record of getting things done (e.g., Tesla). In the US, women are doing much better in college than men: Women earned majority of doctoral…

Stream VByte: breaking new speed records for integer compression

, 8 min read

In many information systems, we work with arrays of integers. For example, maybe you need to keep track of which records in a database contain a given value. This sort of mapping can be expressed as an array of integers. These arrays can end up taking up a large fraction of your memory or disk…

Benchmarking algorithms to visit all values in an array in random order

, 4 min read

In an earlier post, I described how to visit all values in an array in a pseudo-random order quite fast. The idea is simple, given an array of size n, pick a value a that is coprime with n. Then for any value of b, you have that (a x + b ) modulo n will visit all values in [0,n) exactly once as x…

Runtime constants: Go vs. C++

, 2 min read

When programming in C++, you can rely on your compiler to do some interesting optimizations. Let us look at this simple code that sums up the values in an array (passed by pointer): static int length = 10; uint64_t sum(uint64_t * a) { uint64_t s = 0; for(int k = 0; k < length; k++) s…

Runtime constants: Swift

, 2 min read

In an earlier post, I reported that if you have a variable in C++ such that the compiler can determine it to be effectively constant… that is, in practice, we cannot change its value, then the C++ optimizing compiler will treat it as if it were an actual constant. The same thing is not true in a…

Science and Technology links (September 22th, 2017)

, 3 min read

Mass is preserved. When you lose fat, where does the fat goes? It has to go somewhere. No, it is not transformed in “energy” or “heat”. Neither “energy” nor “heat” have atomic mass. I asked the question on social networks and most people got the wrong answer. This YouTube video…

Swift as a low-level programming language?

, 3 min read

Modern processors come with very powerful instructions that are simply not available in many high-level languages JavaScript or Python. Here are a few examples: Most programming languages allow you to multiply two 64-bit integers and to get the 64-bit results (it is typically written as x = a *…

Computing the inverse of odd integers

, 6 min read

Given x, its (multiplicative) inverse is another value y such that xy = yx = 1. We all know that the multiplicative inverse of x is 1/x and it exists as long as x is non-zero. That’s for real numbers, or at least, rational numbers. But the idea of a multiplicative inverse is more general. It…

Visiting all values in an array exactly once in “random order”

, 3 min read

Suppose that you want to visit all values in an array exactly once in “random order”. You could do it by shuffling your array but it requires some extra storage. You want your code to use just a tiny bit of memory, and you want the code to be super fast. You do not want to assume that your…

How fast are malloc_size and malloc_usable_size in C?

, 2 min read

When programming in C, we allocate memory dynamically using the malloc function, by passing how many bytes we wish to allocate. If successful, the function returns a pointer to the newly allocated memory. Later we can free the allocated memory with the free function. In general, unless you are…

Science and Technology links (September 15th, 2017)

, 3 min read

An actual robot that can write in Japanese passed university entrance tests in Japan. How well did it do? It bested 80% of all human candidates. It is not good enough to enter the very best Japanese university, but it is good enough to enter in many other universities. The robot’s artificial…

The Xorshift1024* random number generator fails BigCrush

, 2 min read

In software, we use random number generators to simulate randomness. They take the form of functions which return numbers that appear random. To test their quality, we apply statistical tests. The goal standard is a statical test called BigCrush. It tests that quality of 32-bit random values. In an…

Scholarship is conservative, tinkering is progressive

, 2 min read

We are not rational beings. We cannot string simple sequences of logical arguments without making mistakes. We cannot reason probabilistically. There is ample evidence that rational arguments fail to convince. That’s not surprising given how poor we are at evaluating rational arguments. We teach…

Science and Technology links (September 8th, 2017)

, 2 min read

I always naively assumed that the discovery that the speed of light is finite was recent. Yet back in the 17th century, scientists had already concluded that the speed of light must be finite due to astronomical aberrations. Scientists rejuvenated the brain of old mice using a single protein…

The Xorshift128+ random number generator fails BigCrush

, 6 min read

In software, we generate randomness with algorithms called “pseudo-random number generator”, sometimes simply called “random number generator” or RNG. A popular random number generator is xorshift128+: it is used by many JavaScript engines. It was designed by an influential computer science…

Go does not inline functions when it should

, 4 min read

I have designed a benchmark that I run in different programming languages. Two languages that I like are Go (from Google) and Java (from Oracle). My expectation would be that Go and Java should have similar performance in due time. Indeed, both are modern garbage-collected languages supported by…