[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: New macro 'compf' for composing functions
From: |
Tassilo Horn |
Subject: |
Re: New macro 'compf' for composing functions |
Date: |
Tue, 04 Mar 2025 13:47:31 +0100 |
User-agent: |
mu4e 1.12.9; emacs 31.0.50 |
Tassilo Horn <tsdh@gnu.org> writes:
> These are the numbers I get:
>
> Using compf (compf fn reused)
> Elapsed time: 0.062184s
> Using compf (compf fn used once)
> Elapsed time: 3.740416s (2.795235s in 256 GCs)
> Using compose (composed fn reused)
> Elapsed time: 0.049287s (0.023753s in 2 GCs)
> Using compose (composed fn reused, without compiler macro)
> Elapsed time: 0.044597s (0.021728s in 2 GCs)
> Using compose (composed fn used once)
> Elapsed time: 0.804170s (0.639926s in 59 GCs)
> Using compose (without compiler macro)
> Elapsed time: 0.815339s (0.646015s in 59 GCs)
>
> That suggests that the compiler macro version isn't worth it.
I've now also added Stefan's version
(defun compose (&rest fns)
(require 'cl-lib)
(cl-destructuring-bind (fun . rest) (reverse fns)
(lambda (&rest args)
(seq-reduce (lambda (v f) (funcall f v))
rest
(apply fun args)))))
to the benchmark and get these results for them:
Using compf (compf fn reused)
Elapsed time: 0.062942s
Using compf (compf fn used once)
Elapsed time: 3.778158s (2.818877s in 256 GCs)
Using compose (composed fn reused)
Elapsed time: 0.039880s (0.021969s in 2 GCs)
Using compose (composed fn reused, without compiler macro)
Elapsed time: 0.039624s (0.021752s in 2 GCs)
Using compose (composed fn used once)
Elapsed time: 1.040501s (0.869228s in 79 GCs)
Using compose (without compiler macro)
Elapsed time: 1.041417s (0.866880s in 79 GCs)
Using compose (Stefan’s composed fn reused)
Elapsed time: 0.265133s (0.143706s in 13 GCs)
Using compose (Stefan’s composed fn used once)
Elapsed time: 1.214854s (0.985633s in 89 GCs)
So it seems that both my iterative version as well as Stefan's
functional one are faster than the compf macro in the case where the
composition is only used once which is probably the typical use-case.
In general, the imperative version seems to be the fastest. It's
numbers don't really change when I remove the compiler macro but I don't
know why. (I don't use native-compilation right now, if that matters.)
I guess in the end it's less of a performance question but instead if we
want to invite people to use function composition. As others have said
in this thread, it's much harder to debug than a lambda which does the
composition by hand...
Bye,
Tassilo
- Re: New macro 'compf' for composing functions, Eshel Yaron, 2025/03/01
- Re: New macro 'compf' for composing functions, Eshel Yaron, 2025/03/01
- Re: New macro 'compf' for composing functions, Stefan Kangas, 2025/03/02
- Re: New macro 'compf' for composing functions, Eshel Yaron, 2025/03/02
- Re: New macro 'compf' for composing functions, Stefan Kangas, 2025/03/02
- Re: New macro 'compf' for composing functions, Eshel Yaron, 2025/03/02
- Re: New macro 'compf' for composing functions, Tassilo Horn, 2025/03/03
- Re: New macro 'compf' for composing functions,
Tassilo Horn <=
- Re: New macro 'compf' for composing functions, Alfred M. Szmidt, 2025/03/02
- Re: New macro 'compf' for composing functions, Stefan Kangas, 2025/03/02
- Re: New macro 'compf' for composing functions, tomas, 2025/03/02
- Re: New macro 'compf' for composing functions, Alfred M. Szmidt, 2025/03/02
- Re: New macro 'compf' for composing functions, Stefan Kangas, 2025/03/03
- Re: New macro 'compf' for composing functions, Alfred M. Szmidt, 2025/03/03
- Re: New macro 'compf' for composing functions, Stefan Kangas, 2025/03/03
- Re: New macro 'compf' for composing functions, Eshel Yaron, 2025/03/03
- Re: New macro 'compf' for composing functions, Alfred M. Szmidt, 2025/03/03
- Re: New macro 'compf' for composing functions, Jim Porter, 2025/03/03