How to resolve the algorithm Time a function step by step in the Ada programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Time a function step by step in the Ada programming language
Table of Contents
Problem Statement
Write a program which uses a timer (with the least granularity available on your system) to time how long a function takes to execute. Whenever possible, use methods which measure only the processing time used by the current process; instead of the difference in system time between start and finish, which could include time used by other processes on the computer. This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Time a function step by step in the Ada programming language
Source code in the ada programming language
with Ada.Calendar; use Ada.Calendar;
with Ada.Text_Io; use Ada.Text_Io;
procedure Query_Performance is
type Proc_Access is access procedure(X : in out Integer);
function Time_It(Action : Proc_Access; Arg : Integer) return Duration is
Start_Time : Time := Clock;
Finis_Time : Time;
Func_Arg : Integer := Arg;
begin
Action(Func_Arg);
Finis_Time := Clock;
return Finis_Time - Start_Time;
end Time_It;
procedure Identity(X : in out Integer) is
begin
X := X;
end Identity;
procedure Sum (Num : in out Integer) is
begin
for I in 1..1000 loop
Num := Num + I;
end loop;
end Sum;
Id_Access : Proc_Access := Identity'access;
Sum_Access : Proc_Access := Sum'access;
begin
Put_Line("Identity(4) takes" & Duration'Image(Time_It(Id_Access, 4)) & " seconds.");
Put_Line("Sum(4) takes:" & Duration'Image(Time_It(Sum_Access, 4)) & " seconds.");
end Query_Performance;
You may also check:How to resolve the algorithm Anonymous recursion step by step in the F# programming language
You may also check:How to resolve the algorithm Abbreviations, simple step by step in the Phix programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Tokenize a string step by step in the F# programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the Elixir programming language