From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,71b19e01eae3a390 X-Google-Attributes: gid103376,public From: "Nick Roberts" Subject: Re: delay until and GNAT Date: 1999/05/10 Message-ID: <3736e104@eeyore.callnetuk.com>#1/1 X-Deja-AN: 476172726 References: X-Original-NNTP-Posting-Host: da129d133.dialup.callnetuk.com X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 X-Complaints-To: newsabuse@remarq.com X-Trace: 926343363 02H499TBW8004D443C uk25.supernews.com Organization: RemarQ http://www.remarQ.com Newsgroups: comp.lang.ada Date: 1999-05-10T00:00:00+00:00 List-Id: Okay here it is: with Ada.Calendar, Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO, Ada.Numerics.Elementary_Functions; use Ada.Calendar, Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO, Ada.Numerics.Elementary_Functions; procedure Test_Task_Switching is Tasks: constant := 10; Iterations: constant := 2000; -- package Duration_IO is new Fixed_IO(Duration); task type Test_Task is entry Results (Task_Start, Task_Stop: out Time; Switches: out Natural; Diff_Sum: out Float; Sum_Square: out Float); end; task body Test_Task is Task_Start, Task_Stop: Time; Switches: Natural := 0; Diff_Sum: Float := 0.0; Sum_Square: Float := 0.0; Start: Time; Stop: Time; Diff: Float; begin Put('<'); Task_Start := Clock; for i in Natural range 1..Iterations loop Start := Clock; delay 0.0; -- task switch? Stop := Clock; if Stop /= Start then Switches := Switches + 1; Diff := Float(Stop-Start); Diff_Sum := Diff_Sum + Diff; Sum_Square := Sum_Square + Diff**2; end if; end loop; Task_Stop := Clock; Put('>'); accept Results (Task_Start, Task_Stop: out Time; Switches: out Natural; Diff_Sum: out Float; Sum_Square: out Float) do Task_Start := Test_Task.Task_Start; Task_Stop := Test_Task.Task_Stop; Switches := Test_Task.Switches; Diff_Sum := Test_Task.Diff_Sum; Sum_Square := Test_Task.Sum_Square; end; end Test_Task; Testers: array (1..Tasks) of Test_Task; Task_Start, Task_Stop: Time; Switches: Natural; Diff_Sum: Float; Sum_Square: Float; Grand_Switches: Natural := 0; Grand_Diff_Sum: Float := 0.0; Grand_Sum_Square: Float := 0.0; Average, Std_Dev: Float; begin for i in Testers'Range loop Testers(i).Results(Task_Start,Task_Stop,Switches,Diff_Sum,Sum_Square); Grand_Switches := Grand_Switches + Switches; Grand_Diff_Sum := Grand_Diff_Sum + Diff_Sum; Grand_Sum_Square := Grand_Sum_Square + Sum_Square; end loop; New_Line; Put("Tasks: "); Put(Tasks,Width=>9); New_Line; Put("Iterations: "); Put(Iterations,Width=>9); New_Line; Put("Switches: "); Put(Grand_Switches,Width=>9); New_Line; if Grand_Switches /= 0 then Average := Grand_Diff_Sum/Float(Grand_Switches); Std_Dev := Sqrt(Grand_Sum_Square/Float(Grand_Switches)-Average**2); Put("Average: "); Put(Average,Fore=>2,Aft=>6,Exp=>0); New_Line; Put("Std Dev: "); Put(Std_Dev,Fore=>2,Aft=>6,Exp=>0); New_Line; end if; Put_Line("Program completed."); end; At least two caveats however: (a) the clock used (be it Ada.Calendar or whatever) must be HIGH RESOLUTION (i.e. higher than the lengths of the context switches it's measuring) or the results will be bogus; (b) the heuristic for deciding whether a task switch occurred (assume not if no apparent time elapsed) is potentially dodgy both ways (false negative, false positive) and you should replace it with something better if you can. There are lots of obvious extensions and improvements to this program (e.g. minima and maxima). Have fun. ------------------------------------- Nick Roberts ------------------------------------- Roger Racine wrote in message ... [...] |Please note that all of the problems are associated with the operating |system(s), not GNAT. The bottom line is: if you want to use multitasking, in |any language, and want an upper bound on timing, use a real-time operating |system. [...] You don't necessarily need a real-time operating system, just a half decent one :-)