-- Zeitbetrachtungen verschiedener Fibonacci-Implementierungen with Ada.Calendar,Ada.Integer_text_IO,Ada.text_IO; use Ada.Calendar,Ada.Integer_text_IO,Ada.text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure fibo is pragma Optimize(Time); -------------------- -- User-Interface -- -------------------- function frage_user return Positive is N : Positive; begin Put("N for Fibonacci (i.e. 30) : "); Get(N); New_Line; return N; end frage_user; -------------- -- rek_fibo -- -------------- function rek_fibo(zahl : in Positive) return Positive -- this is old-style standard recursion -- is begin if (zahl < 3) then return 1; else return rek_fibo(zahl-1) + rek_fibo(zahl-2); end if; end rek_fibo; --------------- -- Iteration -- --------------- function it_fibo(wert : in Positive) return Positive is n1 : Positive := 1; n2 : Positive := 1; result : Positive; begin if (wert < 3) then return 1; else for I in 1 .. wert-2 loop result := n1 + n2; n1 := n2; n2 := result; end loop; return result; end if; end it_fibo; ------------- -- ma_fibo -- ------------- function ma_fibo(wert : in Positive) return Positive is wurzel : constant float := 2.23606797749978969640; result : Float; begin result := (1.0/wurzel)*((((1.0+wurzel)/2.0)**wert)- (((1.0-wurzel)/2.0)**wert)); return Integer(result); end ma_fibo; -------------- -- mak_fibo -- -------------- function mak_fibo(wert : in Positive) return Positive is wurzel : constant float := 2.23606797749978969640; bruch : constant float := 1.0 / wurzel; plusw : constant float := (1.0 + wurzel)/2.0; minusw: constant float := (1.0 - wurzel)/2.0; result : Float; begin result := bruch*(plusw**wert-minusw**wert); return Integer(result); end mak_fibo; -------------- -- ma2_fibo -- -------------- function ma2_fibo(wert : in Positive) return Positive is wurzel : constant float := 2.23606797749978969640; result : Float; begin result := (1.0/wurzel)*(((1.0+wurzel)**wert-(1.0-wurzel)**wert)/2.0**wert); return Integer(result); end ma2_fibo; ------------ -- report -- ------------ procedure report (what : in string; dauer : in duration; wert : in Positive; result : in Positive) is begin Put_Line(what & " for " & Positive'Image(wert) & ": Result: " & Positive'Image(result) & " Duration: " &Duration'Image(dauer) & "s"); end report; ---------- -- Main -- ---------- startzeit,endzeit : Time; dauer : Duration; fibowert : Positive; result : Positive; package mymath is new Ada.Numerics.Generic_Elementary_Functions(Float); use mymath; begin fibowert := frage_user; Put_Line("Will do Fibonacci for" & Positive'Image(fibowert)); startzeit := Clock; result := rek_fibo(fibowert); endzeit := Clock; dauer := endzeit - startzeit; report("Standard recursion",dauer,fibowert,result); startzeit := Clock; result := ma_fibo(fibowert); endzeit := Clock; dauer := endzeit - startzeit; report("Mathematics ",dauer,fibowert,result); startzeit := Clock; result := ma2_fibo(fibowert); endzeit := Clock; dauer := endzeit - startzeit; report("Optimized Maths ",dauer,fibowert,result); startzeit := Clock; result := mak_fibo(fibowert); endzeit := Clock; dauer := endzeit - startzeit; report("Fixed-term Maths ",dauer,fibowert,result); startzeit := Clock; result := it_fibo(fibowert); endzeit := Clock; dauer := endzeit - startzeit; report("Iteration ",dauer,fibowert,result); end fibo;