[Haskell-cafe] Maybe won't let me count
Antonio Regidor Garcia
chikitosan at gmail.com
Mon Mar 29 08:38:58 UTC 2021
Mmm strange... I thought I read somewhere that succ is faster, but now I'm searching for the reference and don't find it.
Anyway, good to know!
Best,
Antonio Regidor Garcia
El Mon, Mar 29, 2021 at 04:22:07AM -0400, Viktor Dukhovni escribió:
> > On Mar 29, 2021, at 3:36 AM, Antonio Regidor Garcia <chikitosan at gmail.com> wrote:
> >
> > succ n is n+1 but faster than the function (+).
>
> Because 'succ' typically does bounds checks, while (+) (for Int) just
> does the underlying CPU instruction, that's not particularly plausible.
>
> Indeed running a test (100 million increments) suggests that (+) is
> noticeably cheaper:
>
> (1 +): MUT time 0.035s ( 0.035s elapsed)
> (succ): MUT time 0.134s ( 0.134s elapsed)
>
> The succ function is however safer against uncaught overflow:
>
> λ> succ False
> True
> λ> succ True
> *** Exception: Prelude.Enum.Bool.succ: bad argument
>
> If the datatype in question is not bounded (Double, Integer, ...) then
> succ performance is closer to that of (+). I see identical speeds for
> Double, but (GHC 8.10 on X86_64) succ seems slightly slower for Integer.
>
> --
> Viktor.
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
More information about the Haskell-Cafe
mailing list