3

Following is my quicksort program in Scheme using Racket and I'd like to measure time this program but I cannot find a way to do that.

I tried (time(quicksort(list 1 4 3))) but this is not as precise as I expected. Is there any ideal way to do this?

#lang racket

(define (partition compare lst)
  (cond
     ((null? lst) '())
     ((compare (car lst)) (cons (car lst) (partition compare (cdr lst))))
     (else (partition compare (cdr lst)))))

(define (quicksort lst)
  (cond
     ((null? lst) '())
     (else (let ((pivot (car lst)))
        (append (append (quicksort (partition (lambda (x) (< x pivot)) lst))
                   (partition (lambda (x) (= x pivot)) lst))
                (quicksort (partition (lambda (x) (> x pivot)) lst)))))))
A.Lee
  • 69
  • 3
  • 2
    Please define what does "precision" mean, for your use case. – Óscar López Nov 28 '18 at 09:24
  • 1
    It gives you the real time, CPU time, and time spent in garbage collection all in milliseconds. Do you want something more precise than milliseconds, like microseconds? Because looking at your algorithm it seems like `time`'s milliseconds results should suffice! – Alex V Nov 29 '18 at 00:35
  • 1
    With 3 element list, what are you really testing? `(define small-list (let loop ((n 20000) (acc '())) (if (zero? n) acc (loop (sub1 n) (cons (random 100000) acc)))))` will give a small list with oinlu 200.000 random numeric elements. Remember to not print out the result in the expression but store it as a variable. And create executable and run it since from DrRacket it isn't optimized for speed. – Sylwester Nov 29 '18 at 14:49
  • Thank you guys for yout contributions! – A.Lee Nov 29 '18 at 18:39

0 Answers0