Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Last active December 16, 2021 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Metaxal/21256e7733c96f083a36c4bf52ed525e to your computer and use it in GitHub Desktop.
Save Metaxal/21256e7733c96f083a36c4bf52ed525e to your computer and use it in GitHub Desktop.
andmap stress test
#lang racket
;; Allows for improper lists
(define (my-andmap proc l1 l2)
(let loop ([l1 l1] [l2 l2])
(cond [(and (null? l1) (null? l2)) #t]
[(or (null? l1) (null? l2))
(error "Lists must have the same length" (length l1) (length l2))]
[(and (pair? l1) (pair? l2))
(and (proc (car l1) (car l2))
(loop (cdr l1) (cdr l2)))]
[else
(error "Pairs expected" l1 l2)])))
(begin
(define N 10000)
(define m 1000)
(define ll
(for/list ([i m])
(for/list ([j N])
(random 10))))
(define l1 (first ll))
;; Custom my-andmap: ~7ms
(time
(for/sum ([l1 (in-list ll)])
(if (for/or ([l2 (in-list ll)]
#:unless (eq? l1 l2))
(my-andmap = l1 l2))
1
0)))
;; std andmap: 166s (!)
(time
(for/sum ([l1 (in-list ll)])
(if (for/or ([l2 (in-list ll)]
#:unless (eq? l1 l2))
(andmap = l1 l2))
1
0))))
#|
$ racket andmap-stress-test.rkt
cpu time: 7 real time: 7 gc time: 0
0
cpu time: 166744 real time: 166784 gc time: 176
0
|#
@Metaxal
Copy link
Author

Metaxal commented Dec 16, 2021

The reason is andmap checks the lengths of the lists at each call. But by contrast to list?, length is not memoized, which gives it O(m²N) running time instead of the high-probability O(m²) for the custom my-andmap in this case.

In principle, the length check could be lifted to the top level or be memoized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment