New Prime Number Sieve benchmark


#1

I’ve recently added a new benchmark to test the performance of the uLisp array handling, and in particular the use of bit arrays.

It uses a technique called the Sieve of Eratosthenes to find all the prime numbers up to a specified limit, and it returns the largest one found. A bit array records which numbers are prime. Initially all the elements are ‘0’, but at each stage all the multiples of the next prime are set to ‘1’. The resulting bit array contains a ‘0’ for each prime number:

(defun sieve (size)
  (let ((a (make-array size :element-type 'bit))
        max)
    (setf (aref a 0) 1 (aref a 1) 1)
    (dotimes (i size max)
      (when (zerop (aref a i))
        (setq max i)
        (do ((j (* 2 i) (+ j i))) ((>= j size)) (setf (aref a j) 1))))))

For example, on a Raspberry Pi Pico, to find the largest 5-digit prime:

> (time (sieve 100000))
99991
Time: 20.8 s

See: Benchmarks - Sieve.