I get these timings on x86_64 GNU/Linux using the following profile program.

; A: cpu time: 7444 real time: 7445 gc time: 716
; B: cpu time: 9227 real time: 9228 gc time: 884

(module profile racket/base
(define target-length #e5e4)

(define (A current-list new-element)
(append current-list (list new-element)))

(define (B current-list new-element)
(reverse (cons new-element (reverse current-list))))

(define (profile function)
(printf "~a: " (object-name function))
(time
(void (for/fold ([l null])
([n (in-range target-length)])
(function l n)))))

(profile A)
(profile B))

On 11/16/21 2:04 PM, David Storrs wrote:

> If I want to add an element to the end of an immutable list, is it better to 
> do:
>
> (append current-list (list new-element))
> or
> (reverse (cons new-element (reverse current-list)))
> or
> something else?
>
> I would imagine it's the first one because that should be a single traversal 
> and link add instead of two traversals and rebuilds, but maybe I'm wrong.
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To view this discussion on the web visit 
> [https://groups.google.com/d/msgid/racket-users/CAE8gKocM4jNCg6xKruYwuwFtuOdbOSwugYcgUND1ZD%3DRB1iA0g%40mail.gmail.com](https://groups.google.com/d/msgid/racket-users/CAE8gKocM4jNCg6xKruYwuwFtuOdbOSwugYcgUND1ZD%3DRB1iA0g%40mail.gmail.com?utm_medium=email&utm_source=footer).

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/3a9777e7-4b33-b561-f47e-8fe940699b39%40sagegerard.com.

Reply via email to