unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Tomas Hlavaty <tom@logand.com>
To: Psionic K <psionik@positron.solutions>
Cc: help-gnu-emacs@gnu.org, incal@dataswamp.org,
	Eli Zaretskii <eliz@gnu.org>
Subject: Re: Identifying sources of allocations in a toy Mandelbrot package
Date: Sat, 27 Jan 2024 00:36:41 +0100	[thread overview]
Message-ID: <87wmrvd5va.fsf@neko.mail-host-address-is-not-set> (raw)
In-Reply-To: <CADQMGASHij=pfXv9P97-GymjArDktTrO7vt14JMGPFVkHq0cnQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1302 bytes --]

On Sat 20 Jan 2024 at 19:03, Psionic K <psionik@positron.solutions> wrote:
> I was mainly hoping to start identifying forms that can do okay even
> in spite of the Elisp runtime, especially for these kind of iteration
> problems that could - but should not - generate lots of garbage.  The
> first rows go by in the blink of an eye because of the cache locality.
> Elisp is almost something.  The memory pathologies are likely
> affecting every other perceived lack of responsiveness.

I was curious about:
- How does native compilation improve things?
- How does fixed point arithmetic improve things?

Run:
(benchmark-run 1 (mandelbrot))

with the attached mandelbrot1.el (floating point):
after emacs-lisp-native-compile-and-load:
=> (120.40541861 1947 93.45048212499998)
after emacs-lisp-byte-compile-and-load:
=> (128.362728323 1942 93.44881820700004)

with the attached mandelbrot.el (fixed point):
after emacs-lisp-native-compile-and-load:
(32.898215598 2 0.09366582000001245)
after emacs-lisp-byte-compile-and-load:
(51.629763289 2 0.09165389300000015)

For comparison, the attached forth version test.fs takes about 6sec.

I find it interesting that:
- native compilation improves so little compared to byte compilation.
- floating point calculations are very slow.
- emacs-lisp is very slow


[-- Attachment #2: mandelbrot1.el --]
[-- Type: application/emacs-lisp, Size: 1675 bytes --]

[-- Attachment #3: mandelbrot.el --]
[-- Type: application/emacs-lisp, Size: 2034 bytes --]

[-- Attachment #4: test.fs --]
[-- Type: text/plain, Size: 1122 bytes --]

\ (compile "time gforth test.fs >a.pnm")
1 16 lshift constant 1fx
: fx ( n -- fx ) 1fx * ;
: fx2 ( n1 n2 -- fx ) 1fx swap */ ;
: fx* ( fx1 fx2 -- fx3 ) 1fx */ ;
: fx^2 ( fx -- fx ) dup fx* ;
      1600 constant w     0 value cx
      1200 constant h     0 value cy
-25 10 fx2 constant x0    0 value zr
-15 10 fx2 constant y0    0 value zi
  4 fx w / constant dxp   0 value v
  3 fx h / constant dyp
: cx! ( n -- ) dxp * x0 + to cx ;
: cy! ( n -- ) dyp * y0 + to cy ;
: nl newline type ;
: p6 ." P6" nl w . h . nl 255 . nl ;
: rgb ( n -- ) dup emit dup emit emit ;
\ : expt ( r1 r2 -- r3 ) fexp fswap fln f* ;
: mandelbrot1 ( -- n )
  0 to v   0 to zr   0 to zi
  begin
    v 256 < ( f -- )
    zr fx^2 zi fx^2 + [ 4 fx ] literal <= ( f1 f2 -- )
  and while
    v 1+ to v
    zr fx^2 zi fx^2 - cx + ( fx -- )
    zr zi fx* 2* cy + to zi
    to zr
  repeat
  \ samples that hit 256 wrap in the graphic to display as zero
  \ exponential 0.8 enhances contrast a bit
  v \ s>f 256e f/ 0.8e expt 256e f* floor f>s
  ;
: dot mandelbrot1 rgb ;
: row w 0 do i cx! dot loop ;
: mandelbrot h 0 do i cy! row loop ;
p6 mandelbrot bye

  parent reply	other threads:[~2024-01-26 23:36 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-19  9:19 Identifying sources of allocations in a toy Mandelbrot package Psionic K
2024-01-19 15:33 ` Tomas Hlavaty
2024-01-20  3:14   ` Psionic K
2024-01-20  3:37     ` Psionic K
2024-01-20  7:29     ` Eli Zaretskii
2024-01-20  9:09     ` Tomas Hlavaty
2024-01-20 10:03       ` Psionic K
2024-01-20 10:31         ` Eli Zaretskii
2024-01-26 23:36         ` Tomas Hlavaty [this message]
2024-01-27  1:07           ` Psionic K
2024-01-19 15:44 ` Eli Zaretskii
  -- strict thread matches above, loose matches on Subject: below --
2024-01-27  9:25 Psionic K
2024-01-17 12:39 Psionic K
2024-01-17 12:58 ` Eli Zaretskii
2024-01-17 13:25 ` Emanuel Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wmrvd5va.fsf@neko.mail-host-address-is-not-set \
    --to=tom@logand.com \
    --cc=eliz@gnu.org \
    --cc=help-gnu-emacs@gnu.org \
    --cc=incal@dataswamp.org \
    --cc=psionik@positron.solutions \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).