Jump to content

Rate counter: Difference between revisions

m
→‎{{header|Ada}}: simplify code
(Ada implementation)
m (→‎{{header|Ada}}: simplify code)
Line 14:
 
=={{header|Ada}}==
Launch 6 jobs in parallel and record the elapsed time for each job.<br> A variant
A variant to get CPU times would use the package Ada.Execution_Time (Ada05).The precision<br>
The precision of measure is given by the value of System.Tick; on Windows value is 10 ms.
<lang Ada>with System; use System;
with SystemAda.Text_IO; use SystemAda.Text_IO;
with Ada.Text_IOCalendar; use Ada.Calendar; use Ada.Text_IO;
with Ada.Calendar; use Ada.Calendar; use Ada;
with Ada.Synchronous_Task_Control; use Ada.Synchronous_Task_Control;
 
procedure Rate_Counter is
Line 40 ⟶ 38:
Jobs : array (Job_Index) of Job_Ptr;
 
FlagsDone : Natural := 0;
Completed : array (Job_Index) of Synchronous_Task_Control.Suspension_ObjectBoolean := (others => False);
Completed : array (Job_Index) of Boolean := (others => False);
Start_T, Stop_T : array (Job_Index) of Calendar.Time;
 
Start_T,type Stop_TTimings :is array (Job_Index) of Calendar.Time;
Done : Natural := 0;
Start_T, Stop_T : Timings;
 
task body Job is
Line 51 ⟶ 48:
pragma Volatile (Anchor); -- necessary to avoid compiler optimization.
begin
accept Start do;
-- the job Set_Falseto (Flags (ID));do
for I in Long_Integer'First .. Long_Integer'LastRange loop
end Start;
-- the job to do / could be replaced by
for I in Long_Integer'First .. Long_Integer'Last loop
Anchor := I;
end loop;
Set_True (Flags (ID));
end Job;
 
Line 69 ⟶ 63:
Jobs (J).Start; -- priority settings necessary to regain control.
end loop;
-- Polling for the results / also possible to use a protected type.
while not (Done = Job_Nbr) loop
for J in Job_Index'Range loop
if not Completed (J) and then Current_State (FlagsJobs (J)).all'Terminated then
Stop_T (J) := Calendar.Clock; -- get the end time
Put ("Job #" & Job_Index'Image (J) & " is finished. It took ");
Line 83 ⟶ 77:
delay System.Tick; -- according to the precision of the system clock
end loop;
Duration_IO.Put (System.Tick, Fore => 1, Aft => 3);
Put_Line (" seconds is the precision of System clock.");
 
end Rate_Counter;</lang>
 
Output :
Output :<pre style="overflow: auto; height: 5em;">
Job # 1 is finished. It took 44.49 seconds.
Job # 32 is finished. It took 4414.4305 seconds.
Job # 54 is finished. It took 4414.3205 seconds.
Job # 45 is finished. It took 4414.8505 seconds.
Job # 26 is finished. It took 4514.1705 seconds.
Job # 61 is finished. It took 4414.9915 seconds.</pre>
Job # 13 is finished. It took 4414.4919 seconds.
0.010 seconds is the precision of System clock.</pre>
 
=={{header|C}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.