Function that calculates in compile time when it can

Philippe Sigaud philippe.sigaud at gmail.com
Mon Aug 6 12:06:00 PDT 2012


On Mon, Aug 6, 2012 at 8:37 PM, Russel Winder <russel at winder.org.uk> wrote:
> On Mon, 2012-08-06 at 17:21 +0200, Philippe Sigaud wrote:
> […]
>> Well, you're using the worst possible algorithm to calculate Fibonacci
>> (exponential time), so it's no wonder it's taking foverer :)
>
> Memoization is a bit of a help it destroying that problem.

Memoization at compile-time is a bit tricky, though.

Which reminds me... What if someone wants Fibonacci values depending
on a runtime value, but would like them to be calculated really fast?
Use the "Sabalausky Trick" (aka Nick's Run-Time to Compile-Time Time
Machine): Pregenerate desired values at compile-time (for example here
in an array), to get O(1) fibonacci access at runtime.


More information about the Digitalmars-d-learn mailing list