tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
12
Task/Rate-counter/0DESCRIPTION
Normal file
12
Task/Rate-counter/0DESCRIPTION
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
Counting the frequency at which something occurs is a common activity in measuring performance and managing resources. In this task, we assume that there is some job which we want to perform repeatedly, and we want to know how quickly these jobs are being performed.
|
||||
|
||||
Of interest is the code that performs the actual measurements. Any other code (such as job implementation or dispatching) that is required to demonstrate the rate tracking is helpful, but not the focus.
|
||||
|
||||
Multiple approaches are allowed (even preferable), so long as they can accomplish these goals:
|
||||
|
||||
* Run N seconds worth of jobs and/or Y jobs.
|
||||
* Report at least three distinct times.
|
||||
|
||||
Be aware of the precision and accuracy limitations of your timing mechanisms, and document them if you can.
|
||||
|
||||
'''See also:''' [[System time]], [[Time a function]]
|
||||
71
Task/Rate-counter/Ada/rate-counter.ada
Normal file
71
Task/Rate-counter/Ada/rate-counter.ada
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
with System; use System;
|
||||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Ada.Calendar; use Ada.Calendar;
|
||||
with Ada.Unchecked_Deallocation; use Ada;
|
||||
with Interfaces;
|
||||
|
||||
procedure Rate_Counter is
|
||||
pragma Priority (Max_Priority);
|
||||
|
||||
package Duration_IO is new Fixed_IO (Duration);
|
||||
|
||||
Job_Nbr : constant := 6; -- adjust to your need
|
||||
subtype Job_Index is Natural range 1 .. Job_Nbr;
|
||||
|
||||
task type Job (ID : Job_Index) is
|
||||
pragma Priority (Default_Priority);
|
||||
entry Start;
|
||||
end Job;
|
||||
|
||||
type Job_Ptr is access Job;
|
||||
procedure Free is new Unchecked_Deallocation (Job, Job_Ptr);
|
||||
|
||||
Jobs : array (Job_Index) of Job_Ptr;
|
||||
|
||||
Done : Natural := 0;
|
||||
Completed : array (Job_Index) of Boolean := (others => False);
|
||||
|
||||
type Timings is array (Job_Index) of Calendar.Time;
|
||||
Start_T, Stop_T : Timings;
|
||||
|
||||
task body Job is
|
||||
Anchor : Interfaces.Integer_32;
|
||||
pragma Volatile (Anchor); -- necessary to avoid compiler optimization.
|
||||
begin
|
||||
accept Start;
|
||||
|
||||
for I in Interfaces.Integer_32'Range loop -- the job to do
|
||||
Anchor := I;
|
||||
end loop;
|
||||
end Job;
|
||||
|
||||
begin
|
||||
for J in Job_Index'Range loop
|
||||
Jobs (J) := new Job (ID => J); -- create the jobs first, sync later
|
||||
end loop;
|
||||
for J in Job_Index'Range loop -- launch the jobs in parallel
|
||||
Start_T (J) := Calendar.Clock; -- get the start time
|
||||
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 Jobs (J)'Terminated then
|
||||
Stop_T (J) := Calendar.Clock; -- get the end time
|
||||
Put ("Job #" & Job_Index'Image (J) & " is finished. It took ");
|
||||
Duration_IO.Put (Stop_T (J) - Start_T (J), Fore => 3, Aft => 2);
|
||||
Put_Line (" seconds.");
|
||||
Completed (J) := True;
|
||||
Done := Done + 1;
|
||||
end if;
|
||||
end loop;
|
||||
delay System.Tick; -- according to the precision of the system clock
|
||||
end loop;
|
||||
Duration_IO.Put (System.Tick, Fore => 1, Aft => 6);
|
||||
Put_Line (" seconds is the precision of System clock.");
|
||||
|
||||
for J in Job_Index'Range loop
|
||||
Free (Jobs (J)); -- no GC in Ada, clean-up is explicit
|
||||
end loop;
|
||||
|
||||
end Rate_Counter;
|
||||
26
Task/Rate-counter/BBC-BASIC/rate-counter.bbc
Normal file
26
Task/Rate-counter/BBC-BASIC/rate-counter.bbc
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
PRINT "Method 1: Calculate reciprocal of elapsed time:"
|
||||
FOR trial% = 1 TO 3
|
||||
start% = TIME
|
||||
PROCtasktomeasure
|
||||
finish% = TIME
|
||||
PRINT "Rate = "; 100 / (finish%-start%) " per second"
|
||||
NEXT trial%
|
||||
|
||||
PRINT '"Method 2: Count completed tasks in one second:"
|
||||
FOR trial% = 1 TO 3
|
||||
runs% = 0
|
||||
finish% = TIME + 100
|
||||
REPEAT
|
||||
PROCtasktomeasure
|
||||
IF TIME < finish% runs% += 1
|
||||
UNTIL TIME >= finish%
|
||||
PRINT "Rate = "; runs% " per second"
|
||||
NEXT trial%
|
||||
END
|
||||
|
||||
REM This is an example, replace with the task you want to measure
|
||||
DEF PROCtasktomeasure
|
||||
LOCAL i%
|
||||
FOR i% = 1 TO 1000000
|
||||
NEXT
|
||||
ENDPROC
|
||||
78
Task/Rate-counter/C++/rate-counter.cpp
Normal file
78
Task/Rate-counter/C++/rate-counter.cpp
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#include <iostream>
|
||||
#include <ctime>
|
||||
|
||||
// We only get one-second precision on most systems, as
|
||||
// time_t only holds seconds.
|
||||
class CRateState
|
||||
{
|
||||
protected:
|
||||
time_t m_lastFlush;
|
||||
time_t m_period;
|
||||
size_t m_tickCount;
|
||||
public:
|
||||
CRateState(time_t period);
|
||||
void Tick();
|
||||
};
|
||||
|
||||
CRateState::CRateState(time_t period) : m_lastFlush(std::time(NULL)),
|
||||
m_period(period),
|
||||
m_tickCount(0)
|
||||
{ }
|
||||
|
||||
void CRateState::Tick()
|
||||
{
|
||||
m_tickCount++;
|
||||
|
||||
time_t now = std::time(NULL);
|
||||
|
||||
if((now - m_lastFlush) >= m_period)
|
||||
{
|
||||
//TPS Report
|
||||
size_t tps = 0.0;
|
||||
if(m_tickCount > 0)
|
||||
tps = m_tickCount / (now - m_lastFlush);
|
||||
|
||||
std::cout << tps << " tics per second" << std::endl;
|
||||
|
||||
//Reset
|
||||
m_tickCount = 0;
|
||||
m_lastFlush = now;
|
||||
}
|
||||
}
|
||||
|
||||
// A stub function that simply represents whatever it is
|
||||
// that we want to multiple times.
|
||||
void something_we_do()
|
||||
{
|
||||
// We use volatile here, as many compilers will optimize away
|
||||
// the for() loop otherwise, even without optimizations
|
||||
// explicitly enabled.
|
||||
//
|
||||
// volatile tells the compiler not to make any assumptions
|
||||
// about the variable, implying that the programmer knows more
|
||||
// about that variable than the compiler, in this case.
|
||||
volatile size_t anchor = 0;
|
||||
for(size_t x = 0; x < 0xffff; ++x)
|
||||
{
|
||||
anchor = x;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
time_t start = std::time(NULL);
|
||||
|
||||
CRateState rateWatch(5);
|
||||
|
||||
// Loop for twenty seconds
|
||||
for(time_t latest = start; (latest - start) < 20; latest = std::time(NULL))
|
||||
{
|
||||
// Do something.
|
||||
something_we_do();
|
||||
|
||||
// Note that we did something.
|
||||
rateWatch.Tick();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
74
Task/Rate-counter/C/rate-counter.c
Normal file
74
Task/Rate-counter/C/rate-counter.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
// We only get one-second precision on most systems, as
|
||||
// time_t only holds seconds.
|
||||
struct rate_state_s
|
||||
{
|
||||
time_t lastFlush;
|
||||
time_t period;
|
||||
size_t tickCount;
|
||||
};
|
||||
|
||||
void tic_rate(struct rate_state_s* pRate)
|
||||
{
|
||||
pRate->tickCount += 1;
|
||||
|
||||
time_t now = time(NULL);
|
||||
|
||||
if((now - pRate->lastFlush) >= pRate->period)
|
||||
{
|
||||
//TPS Report
|
||||
size_t tps = 0.0;
|
||||
if(pRate->tickCount > 0)
|
||||
tps = pRate->tickCount / (now - pRate->lastFlush);
|
||||
|
||||
printf("%u tics per second.\n", tps);
|
||||
|
||||
//Reset
|
||||
pRate->tickCount = 0;
|
||||
pRate->lastFlush = now;
|
||||
}
|
||||
}
|
||||
|
||||
// A stub function that simply represents whatever it is
|
||||
// that we want to multiple times.
|
||||
void something_we_do()
|
||||
{
|
||||
// We use volatile here, as many compilers will optimize away
|
||||
// the for() loop otherwise, even without optimizations
|
||||
// explicitly enabled.
|
||||
//
|
||||
// volatile tells the compiler not to make any assumptions
|
||||
// about the variable, implying that the programmer knows more
|
||||
// about that variable than the compiler, in this case.
|
||||
volatile size_t anchor = 0;
|
||||
size_t x = 0;
|
||||
for(x = 0; x < 0xffff; ++x)
|
||||
{
|
||||
anchor = x;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
time_t start = time(NULL);
|
||||
|
||||
struct rate_state_s rateWatch;
|
||||
rateWatch.lastFlush = start;
|
||||
rateWatch.tickCount = 0;
|
||||
rateWatch.period = 5; // Report every five seconds.
|
||||
|
||||
time_t latest = start;
|
||||
// Loop for twenty seconds
|
||||
for(latest = start; (latest - start) < 20; latest = time(NULL))
|
||||
{
|
||||
// Do something.
|
||||
something_we_do();
|
||||
|
||||
// Note that we did something.
|
||||
tic_rate(&rateWatch);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
Task/Rate-counter/Common-Lisp/rate-counter-1.lisp
Normal file
1
Task/Rate-counter/Common-Lisp/rate-counter-1.lisp
Normal file
|
|
@ -0,0 +1 @@
|
|||
(time (do some stuff))
|
||||
10
Task/Rate-counter/Common-Lisp/rate-counter-2.lisp
Normal file
10
Task/Rate-counter/Common-Lisp/rate-counter-2.lisp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(defmacro time-this (cnt &rest body)
|
||||
(let ((real-t (gensym)) (run-t (gensym)))
|
||||
`(let (,real-t ,run-t)
|
||||
(setf ,real-t (get-internal-real-time)
|
||||
,run-t (get-internal-run-time))
|
||||
(loop repeat ,cnt do ,@body)
|
||||
(list (/ (- (get-internal-real-time) ,real-t)
|
||||
(coerce internal-time-units-per-second 'float))
|
||||
(/ (- (get-internal-run-time) ,run-t)
|
||||
(coerce internal-time-units-per-second 'float))))))
|
||||
1
Task/Rate-counter/Common-Lisp/rate-counter-3.lisp
Normal file
1
Task/Rate-counter/Common-Lisp/rate-counter-3.lisp
Normal file
|
|
@ -0,0 +1 @@
|
|||
(print (time-this 99 (loop for i below 10000 sum i)))
|
||||
1
Task/Rate-counter/Common-Lisp/rate-counter-4.lisp
Normal file
1
Task/Rate-counter/Common-Lisp/rate-counter-4.lisp
Normal file
|
|
@ -0,0 +1 @@
|
|||
(0.023 0.022)
|
||||
24
Task/Rate-counter/E/rate-counter-1.e
Normal file
24
Task/Rate-counter/E/rate-counter-1.e
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
def makeLamportSlot := <import:org.erights.e.elib.slot.makeLamportSlot>
|
||||
|
||||
The rate counter:
|
||||
|
||||
/** Returns a function to call to report the event being counted, and an
|
||||
EverReporter slot containing the current rate, as a float64 in units of
|
||||
events per millisecond. */
|
||||
def makeRateCounter(timer, reportPeriod) {
|
||||
var count := 0
|
||||
var start := timer.now()
|
||||
def &rate := makeLamportSlot(nullOk[float64], null)
|
||||
|
||||
def signal() {
|
||||
def time := timer.now()
|
||||
count += 1
|
||||
if (time >= start + reportPeriod) {
|
||||
rate := count / (time - start)
|
||||
start := time
|
||||
count := 0
|
||||
}
|
||||
}
|
||||
|
||||
return [signal, &rate]
|
||||
}
|
||||
34
Task/Rate-counter/E/rate-counter-2.e
Normal file
34
Task/Rate-counter/E/rate-counter-2.e
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/** Dummy task: Retrieve http://localhost/ and return the content. */
|
||||
def theJob() {
|
||||
return when (def text := <http://localhost/> <- getText()) -> {
|
||||
text
|
||||
}
|
||||
}
|
||||
|
||||
/** Repeatedly run 'action' and wait for it until five seconds have elapsed. */
|
||||
def repeatForFiveSeconds(action) {
|
||||
def stopTime := timer.now() + 5000
|
||||
def loop() {
|
||||
if (timer.now() < stopTime) {
|
||||
when (action <- ()) -> {
|
||||
loop()
|
||||
}
|
||||
}
|
||||
}
|
||||
loop()
|
||||
}
|
||||
|
||||
def whenever := <import:org.erights.e.elib.slot.whenever>
|
||||
|
||||
def [signal, &rate] := makeRateCounter(timer, 1000)
|
||||
|
||||
# Prepare to report the rate info.
|
||||
whenever([&rate], fn {
|
||||
println(`Rate: ${rate*1000} requests/sec`)
|
||||
}, fn {true})
|
||||
|
||||
# Do some stuff to be counted.
|
||||
repeatForFiveSeconds(fn {
|
||||
signal()
|
||||
theJob()
|
||||
})
|
||||
52
Task/Rate-counter/Go/rate-counter.go
Normal file
52
Task/Rate-counter/Go/rate-counter.go
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// representation of time.Time is nanosecond, actual resolution system specific
|
||||
type rateStateS struct {
|
||||
lastFlush time.Time
|
||||
period time.Duration
|
||||
tickCount int
|
||||
}
|
||||
|
||||
func ticRate(pRate *rateStateS) {
|
||||
pRate.tickCount++
|
||||
now := time.Now()
|
||||
if now.Sub(pRate.lastFlush) >= pRate.period {
|
||||
// TPS Report
|
||||
tps := 0.
|
||||
if pRate.tickCount > 0 {
|
||||
tps = float64(pRate.tickCount) / now.Sub(pRate.lastFlush).Seconds()
|
||||
}
|
||||
fmt.Println(tps, "tics per second.")
|
||||
|
||||
// Reset
|
||||
pRate.tickCount = 0
|
||||
pRate.lastFlush = now
|
||||
}
|
||||
}
|
||||
|
||||
func somethingWeDo() {
|
||||
time.Sleep(time.Duration(9e7 + rand.Int63n(2e7))) // sleep about .1 second.
|
||||
}
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
|
||||
rateWatch := rateStateS{
|
||||
lastFlush: start,
|
||||
period: 5 * time.Second,
|
||||
}
|
||||
|
||||
// Loop for twenty seconds
|
||||
latest := start
|
||||
for latest.Sub(start) < 20*time.Second {
|
||||
somethingWeDo()
|
||||
ticRate(&rateWatch)
|
||||
latest = time.Now()
|
||||
}
|
||||
}
|
||||
20
Task/Rate-counter/HicEst/rate-counter.hicest
Normal file
20
Task/Rate-counter/HicEst/rate-counter.hicest
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
CHARACTER prompt='Count "Hits++" for 5 sec, get current rate'
|
||||
|
||||
DLG(Button="1:&Hits++", CALL="cb", B="2:&Count 5sec", B="3:&Rate", RC=retcod, TItle=prompt, WIN=hdl)
|
||||
|
||||
SUBROUTINE cb ! callback after dialog buttons
|
||||
IF(retcod == 1) THEN ! "Hits++" button
|
||||
Hits = Hits + 1
|
||||
ELSEIF(retcod == 2) THEN ! "Count 5 sec" button
|
||||
Hits = 0
|
||||
ALARM(5, 5) ! call F5 in 5 seconds
|
||||
t_start = TIME()
|
||||
ELSE ! "Rate" button
|
||||
sec = TIME() - t_start
|
||||
WRITE(StatusBar) 'Average rate since last "5 sec" button = ', hits/sec, " Hz"
|
||||
ENDIF
|
||||
END
|
||||
|
||||
SUBROUTINE F5 ! called 5 sec after button "5 sec"
|
||||
WRITE(StatusBar) Hits, "hits last 5 sec"
|
||||
END
|
||||
1
Task/Rate-counter/J/rate-counter-1.j
Normal file
1
Task/Rate-counter/J/rate-counter-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
x (6!:2) y
|
||||
4
Task/Rate-counter/J/rate-counter-2.j
Normal file
4
Task/Rate-counter/J/rate-counter-2.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
list=: 1e6 ?@$ 100 NB. 1 million random integers from 0 to 99
|
||||
freqtable=: ~. ,. #/.~ NB. verb to calculate and build frequency table
|
||||
20 (6!:2) 'freqtable list' NB. calculate and build frequency table for list, 20 times
|
||||
0.00994106
|
||||
2
Task/Rate-counter/J/rate-counter-3.j
Normal file
2
Task/Rate-counter/J/rate-counter-3.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
1 1 1 (6!:2) 'freqtable list'
|
||||
0.0509995 0.0116702 0.0116266
|
||||
14
Task/Rate-counter/JavaScript/rate-counter.js
Normal file
14
Task/Rate-counter/JavaScript/rate-counter.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function millis() { // Gets current time in milliseconds.
|
||||
return (new Date()).getTime();
|
||||
}
|
||||
|
||||
/* Executes function 'func' n times, returns array of execution times. */
|
||||
function benchmark(n, func, args) {
|
||||
var times = [];
|
||||
for (var i=0; i<n; i++) {
|
||||
var m = millis();
|
||||
func.apply(func, args);
|
||||
times.push(millis() - m);
|
||||
}
|
||||
return times;
|
||||
}
|
||||
56
Task/Rate-counter/Liberty-BASIC/rate-counter.liberty
Normal file
56
Task/Rate-counter/Liberty-BASIC/rate-counter.liberty
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Print "Rate counter"
|
||||
print "Precision: system clock, ms ";
|
||||
t0=time$("ms")
|
||||
while time$("ms")=t0 'busy loop till click ticks
|
||||
wend
|
||||
print time$("ms")-t0
|
||||
print
|
||||
|
||||
Print "Run jobs N times, report every time"
|
||||
Print "After that, report average time"
|
||||
N=10
|
||||
t00=time$("ms")
|
||||
for i = 1 to 10
|
||||
scan
|
||||
t0=time$("ms")
|
||||
'any code we want to measure goes here
|
||||
res = testFunc()
|
||||
'end of measured code
|
||||
t1=time$("ms")
|
||||
ElapsedTime = t1-t0
|
||||
print "Job #";i;" Elapsed time, ms ";ElapsedTime, 1000/ElapsedTime; " ticks per second"
|
||||
next
|
||||
print "---------------------------------"
|
||||
print "Average time, ms, is ";(t1-t00)/N, 1000/((t1-t00)/N); " ticks per second"
|
||||
|
||||
|
||||
print
|
||||
print "Run jobs for not less then N seconds (if time up, it'll finish last job)"
|
||||
print "After that, report average time"
|
||||
|
||||
NSec=5
|
||||
i = 0
|
||||
t00=time$("ms")
|
||||
while time$("ms")<t00+NSec*1000
|
||||
scan
|
||||
i = i+1
|
||||
t0=time$("ms")
|
||||
'any code we want to measure goes here
|
||||
res = testFunc()
|
||||
'end of measured code
|
||||
t1=time$("ms")
|
||||
ElapsedTime = t1-t0
|
||||
print "Job #";i;" Elapsed time, ms ";ElapsedTime, 1000/ElapsedTime; " ticks per second"
|
||||
wend
|
||||
print "---------------------------------"
|
||||
print "Average time, ms, is ";(t1-t00)/i, 1000/((t1-t00)/i); " ticks per second"
|
||||
|
||||
end
|
||||
|
||||
function testFunc()
|
||||
s=0
|
||||
for i = 1 to 30000
|
||||
s=s+sin(i)/30000
|
||||
next
|
||||
testFunc = s
|
||||
end function
|
||||
118
Task/Rate-counter/OxygenBasic/rate-counter.oxy
Normal file
118
Task/Rate-counter/OxygenBasic/rate-counter.oxy
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
'========
|
||||
'TIME API
|
||||
'========
|
||||
|
||||
'http://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspx
|
||||
|
||||
extern lib "kernel32.dll"
|
||||
|
||||
type SYSTEMTIME
|
||||
WORD wYear
|
||||
WORD wMonth
|
||||
WORD wDayOfWeek
|
||||
WORD wDay
|
||||
WORD wHour
|
||||
WORD wMinute
|
||||
WORD wSecond
|
||||
WORD wMilliseconds
|
||||
end type
|
||||
|
||||
void GetSystemTime(SYSTEMTIME*t)
|
||||
void GetLocalTime(SYSTEMTIME*t)
|
||||
void QueryPerformanceCounter(quad*c)
|
||||
void QueryPerformanceFrequency(quad*freq)
|
||||
void Sleep(sys millisecods)
|
||||
|
||||
end extern
|
||||
|
||||
String WeekDay[7]={"Sunday","Monday","Tuesday","Wednesday",
|
||||
"Thursday","Friday","Saturday"}
|
||||
|
||||
String MonthName[12]={"January","February","March","April","May","June",
|
||||
"July","August","September","October","November","December"}
|
||||
|
||||
|
||||
'==============
|
||||
Class Jobrecord
|
||||
'==============
|
||||
|
||||
has SYSTEMTIME stt
|
||||
has SYSTEMTIME fin
|
||||
quad countA
|
||||
quad CountB
|
||||
quad freq
|
||||
sys serial
|
||||
|
||||
method pad(string s) as string
|
||||
method=s
|
||||
if len(method)<2 then method="0"+method
|
||||
end method
|
||||
|
||||
|
||||
method ShowDateTime(sys a,f) as string
|
||||
|
||||
SYSTEMTIME *t
|
||||
|
||||
if a then
|
||||
@t=@fin
|
||||
else
|
||||
@t=@stt
|
||||
end if
|
||||
'
|
||||
String month=pad(str t.wMonth)
|
||||
String day=pad(str t.wDay)
|
||||
if f=0 then
|
||||
return "" t.wYear "-" month "-" day " "+
|
||||
pad(t.wHour) ":" pad(t.wMinute) ":" pad(t.wSecond) ":" t.wMilliSeconds
|
||||
elseif f=1
|
||||
return WeekDay[t.wDayOfWeek+1 and 7 ] " " +
|
||||
MonthName[t.wMonth and 31] " " day " " t.wYear
|
||||
end if
|
||||
end method
|
||||
|
||||
method Start()
|
||||
QueryPerformanceCounter countA
|
||||
QueryPerformanceFrequency freq
|
||||
serial++
|
||||
GetLocalTime stt
|
||||
end method
|
||||
|
||||
method Finish()
|
||||
GetLocalTime fin
|
||||
QueryPerformanceCounter countB
|
||||
end method
|
||||
|
||||
|
||||
method ShowDuration() as string
|
||||
return str((countB-countA)/freq,6) 'seconds with microsecond resolution
|
||||
end method
|
||||
|
||||
method report() as string
|
||||
string tab=chr(9), cr=chr(13)+chr(10)
|
||||
method="Job:" tab serial cr +
|
||||
"Duration:" tab ShowDuration() cr +
|
||||
"Start: " tab ShowDateTime(0,0) cr +
|
||||
"Finish:" tab ShowDateTime(1,0) cr +
|
||||
ShowDateTime(1,1) cr
|
||||
end method
|
||||
|
||||
end class
|
||||
|
||||
'#recordof JobRecord
|
||||
|
||||
'====
|
||||
'TEST
|
||||
'====
|
||||
|
||||
JobRecord JR
|
||||
JR.start
|
||||
sleep 100 'JOB!
|
||||
JR.finish
|
||||
print JR.Report
|
||||
'putfile "s.txt",JR.Report
|
||||
'
|
||||
'Job: 1
|
||||
'Duration: 0.099026
|
||||
'Start: 2012-07-01 00:52:36:874
|
||||
'Finish: 2012-07-01 00:52:36:974
|
||||
'Sunday July 01 2012
|
||||
10
Task/Rate-counter/PARI-GP/rate-counter.pari
Normal file
10
Task/Rate-counter/PARI-GP/rate-counter.pari
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
a=0;
|
||||
b=0;
|
||||
for(n=1,20000000,
|
||||
a=a+gettime();
|
||||
if(a>60000,print(b);a=0;b=0);
|
||||
'''code to test'''
|
||||
b=b+1;
|
||||
a=a+gettime();
|
||||
if(a>60000,print(b);a=0;b=0)
|
||||
)
|
||||
20
Task/Rate-counter/Perl-6/rate-counter.pl6
Normal file
20
Task/Rate-counter/Perl-6/rate-counter.pl6
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
sub runrate($N where $N > 0, &todo) {
|
||||
my $n = $N;
|
||||
|
||||
my $start = now;
|
||||
todo() while --$n;
|
||||
my $end = now;
|
||||
|
||||
say "Start time: ", DateTime.new($start).Str;
|
||||
say "End time: ", DateTime.new($end).Str;
|
||||
my $elapsed = $end - $start;
|
||||
|
||||
say "Elapsed time: $elapsed seconds";
|
||||
say "Rate: { ($N / $elapsed).fmt('%.2f') } per second\n";
|
||||
}
|
||||
|
||||
sub factorial($n) { (state @)[$n] //= $n < 2 ?? 1 !! $n * factorial($n-1) }
|
||||
|
||||
runrate 10000, { state $n = 1; factorial($n++) }
|
||||
|
||||
runrate 10000, { state $n = 1; factorial($n++) }
|
||||
12
Task/Rate-counter/Perl/rate-counter.pl
Normal file
12
Task/Rate-counter/Perl/rate-counter.pl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
use Benchmark;
|
||||
|
||||
timethese COUNT,{ 'Job1' => &job1, 'Job2' => &job2 };
|
||||
|
||||
sub job1
|
||||
{
|
||||
...job1 code...
|
||||
}
|
||||
sub job2
|
||||
{
|
||||
...job2 code...
|
||||
}
|
||||
8
Task/Rate-counter/PicoLisp/rate-counter-1.l
Normal file
8
Task/Rate-counter/PicoLisp/rate-counter-1.l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(prin "Hit a key ... ")
|
||||
(key)
|
||||
(prinl)
|
||||
(let Usec (usec)
|
||||
(prin "Hit another key ... ")
|
||||
(key)
|
||||
(prinl)
|
||||
(prinl "This took " (format (- (usec) Usec) 6) " seconds") )
|
||||
1
Task/Rate-counter/PicoLisp/rate-counter-2.l
Normal file
1
Task/Rate-counter/PicoLisp/rate-counter-2.l
Normal file
|
|
@ -0,0 +1 @@
|
|||
(bench (key))
|
||||
35
Task/Rate-counter/PureBasic/rate-counter-1.purebasic
Normal file
35
Task/Rate-counter/PureBasic/rate-counter-1.purebasic
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
Procedure.d TimesPSec(Reset=#False)
|
||||
Static starttime, cnt
|
||||
Protected Result.d, dt
|
||||
If Reset
|
||||
starttime=ElapsedMilliseconds(): cnt=0
|
||||
Else
|
||||
cnt+1
|
||||
dt=(ElapsedMilliseconds()-starttime)
|
||||
If dt
|
||||
Result=cnt/(ElapsedMilliseconds()-starttime)
|
||||
EndIf
|
||||
EndIf
|
||||
ProcedureReturn Result*1000
|
||||
EndProcedure
|
||||
|
||||
If OpenWindow(0,#PB_Ignore,#PB_Ignore,220,110,"",#PB_Window_SystemMenu)
|
||||
Define Event, r.d, GadgetNumber
|
||||
ButtonGadget(0,10, 5,200,35,"Click me!")
|
||||
ButtonGadget(1,10,70,100,35,"Reset")
|
||||
TextGadget (2,10,45,200,25,"")
|
||||
TimesPSec(1)
|
||||
Repeat
|
||||
Event=WaitWindowEvent()
|
||||
If Event=#PB_Event_Gadget
|
||||
GadgetNumber =EventGadget()
|
||||
If GadgetNumber=0
|
||||
r=TimesPSec()
|
||||
SetGadgetText(2,"You are clicking at "+StrD(r,5)+" Hz.")
|
||||
ElseIf GadgetNumber=1
|
||||
TimesPSec(1)
|
||||
SetGadgetText(2,"Counter zeroed.")
|
||||
EndIf
|
||||
EndIf
|
||||
Until Event=#PB_Event_CloseWindow
|
||||
EndIf
|
||||
13
Task/Rate-counter/PureBasic/rate-counter-2.purebasic
Normal file
13
Task/Rate-counter/PureBasic/rate-counter-2.purebasic
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
Procedure DummyThread(arg)
|
||||
Define.d dummy=#PI*Pow(arg,2)/4
|
||||
EndProcedure
|
||||
|
||||
start=ElapsedMilliseconds()
|
||||
Repeat
|
||||
T=CreateThread(@DummyThread(),Random(100))
|
||||
WaitThread(T)
|
||||
cnt+1
|
||||
Until start+10000<=ElapsedMilliseconds(); Count for 10 sec
|
||||
|
||||
msg$="We got "+Str(cnt)+" st."+Chr(10)+StrF(cnt/10,2)+" threads per sec."
|
||||
MessageRequester("Counting threads in 10 sec",msg$)
|
||||
58
Task/Rate-counter/Python/rate-counter.py
Normal file
58
Task/Rate-counter/Python/rate-counter.py
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import subprocess
|
||||
import time
|
||||
|
||||
class Tlogger(object):
|
||||
def __init__(self):
|
||||
self.counts = 0
|
||||
self.tottime = 0.0
|
||||
self.laststart = 0.0
|
||||
self.lastreport = time.time()
|
||||
|
||||
def logstart(self):
|
||||
self.laststart = time.time()
|
||||
|
||||
def logend(self):
|
||||
self.counts +=1
|
||||
self.tottime += (time.time()-self.laststart)
|
||||
if (time.time()-self.lastreport)>5.0: # report once every 5 seconds
|
||||
self.report()
|
||||
|
||||
def report(self):
|
||||
if ( self.counts > 4*self.tottime):
|
||||
print "Subtask execution rate: %f times/second"% (self.counts/self.tottime);
|
||||
else:
|
||||
print "Average execution time: %f seconds"%(self.tottime/self.counts);
|
||||
self.lastreport = time.time()
|
||||
|
||||
|
||||
def taskTimer( n, subproc_args ):
|
||||
logger = Tlogger()
|
||||
|
||||
for x in range(n):
|
||||
logger.logstart()
|
||||
p = subprocess.Popen(subproc_args)
|
||||
p.wait()
|
||||
logger.logend()
|
||||
logger.report()
|
||||
|
||||
|
||||
import timeit
|
||||
import sys
|
||||
|
||||
def main( ):
|
||||
|
||||
# for accurate timing of code segments
|
||||
s = """j = [4*n for n in range(50)]"""
|
||||
timer = timeit.Timer(s)
|
||||
rzlts = timer.repeat(5, 5000)
|
||||
for t in rzlts:
|
||||
print "Time for 5000 executions of statement = ",t
|
||||
|
||||
# subprocess execution timing
|
||||
print "#times:",sys.argv[1]
|
||||
print "Command:",sys.argv[2:]
|
||||
print ""
|
||||
for k in range(3):
|
||||
taskTimer( int(sys.argv[1]), sys.argv[2:])
|
||||
|
||||
main()
|
||||
36
Task/Rate-counter/REXX/rate-counter.rexx
Normal file
36
Task/Rate-counter/REXX/rate-counter.rexx
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*REXX program reports on the time 4 different tasks take (wall clock)*/
|
||||
time.= /*nullify times for all tasks. */
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
call time 'Reset' /*reset the REXX (elapsed) timer.*/
|
||||
/*show π in hexadecimal to */
|
||||
/*2,000 decimal places. */
|
||||
task.1='base(pi,16)'
|
||||
call '$CALC' task.1 /*perform task number one. */
|
||||
time.1=time('Elapsed') /*save the time used by task 1. */
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
call time 'Reset' /*reset the REXX (elapsed) timer.*/
|
||||
/*get primes # 40000──►40800 and */
|
||||
/*show their differences. */
|
||||
task.2='diffs[prime(40k,40.8k)] ;;; group 20'
|
||||
call '$CALC' task.2 /*perform task number two. */
|
||||
time.2=time('Elapsed') /*save the time used by task 2. */
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
call time 'Reset' /*reset the REXX (elapsed) timer.*/
|
||||
/*show the Collatz sequence for */
|
||||
/*a stupidly big number. */
|
||||
task.3='collatz(38**8) ;;; Horizontal'
|
||||
call '$CALC' task.3 /*perform task number three. */
|
||||
time.3=time('Elapsed') /*save the time used by task 3. */
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
call time 'Reset' /*reset the REXX (elapsed) timer.*/
|
||||
/*plot SIN in ½ degree increments*/
|
||||
/*using 9 decimal digits (¬ 60).*/
|
||||
task.4='sind(-180,+180,0.5) ;;; Plot DIGits 9'
|
||||
call '$CALC' task.4 /*perform task number four. */
|
||||
time.4=time('Elapsed') /*save the time used by task 4. */
|
||||
/*──────────────────────────────────────────────────────────────────────*/
|
||||
say
|
||||
do j=1 while time.j\==''
|
||||
say 'time used for task' j "was" right(format(time.j,,0),4) 'seconds.'
|
||||
end /*j*/
|
||||
/*stick a fork in it, we're done.*/
|
||||
45
Task/Rate-counter/Run-BASIC/rate-counter.run
Normal file
45
Task/Rate-counter/Run-BASIC/rate-counter.run
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
html "<table bgcolor=wheat border=1><tr><td align=center colspan=2>Rate Counter</td></tr>
|
||||
<tr><td>Run Job Times</td><td>"
|
||||
textbox #runTimes,"10",3
|
||||
|
||||
html "</tr><tr><td align=center colspan=2>"
|
||||
button #r,"Run", [runIt]
|
||||
html " "
|
||||
button #a, "Average", [ave]
|
||||
html "</td></tr></table>"
|
||||
wait
|
||||
|
||||
[runIt]
|
||||
runTimes = min(10,val(#runTimes contents$()))
|
||||
count = count + 1
|
||||
print "-------- Run Number ";count;" ----------------"
|
||||
print "Run jobs";runTimes;" times, reporting each"
|
||||
|
||||
for i = 1 to runTimes
|
||||
' -----------------------------------------------------------------
|
||||
' Normally we use a RUN() command to run another program
|
||||
' but for test pruporse we have a routine that simply loops a bunch
|
||||
' -----------------------------------------------------------------
|
||||
begTime = time$("ms")
|
||||
theRun = bogusProg()
|
||||
|
||||
endTime = time$("ms")
|
||||
lapsTime = endTime - begTime
|
||||
print "Job #";i;" Elapsed time, ms ";lapsTime;" ";1000/lapsTime; " ticks per second"
|
||||
next
|
||||
aveTime = (endTime-startTime)/runTimes
|
||||
totAveTime = totAveTime + aveTime
|
||||
print "Average time, ms, is ";aveTime;" "; 1000/((endTime-startTime)/runTimes); " ticks per second"
|
||||
wait
|
||||
|
||||
[ave]
|
||||
print "---------------------------------"
|
||||
print "Total average time:";aveTime/count
|
||||
|
||||
function bogusProg()
|
||||
for i = 1 to 10000
|
||||
sini = sini + sin(i)
|
||||
tani = tani + tan(i)
|
||||
cpsi = cosi + cos(i)
|
||||
next
|
||||
end function
|
||||
11
Task/Rate-counter/Scala/rate-counter-1.scala
Normal file
11
Task/Rate-counter/Scala/rate-counter-1.scala
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def task(n: Int) = Thread.sleep(n * 1000)
|
||||
def rate(fs: List[() => Unit]) = {
|
||||
val jobs = fs map (f => scala.actors.Futures.future(f()))
|
||||
val cnt1 = scala.actors.Futures.awaitAll(5000, jobs: _*).count(_ != None)
|
||||
val cnt2 = scala.actors.Futures.awaitAll(5000, jobs: _*).count(_ != None)
|
||||
val cnt3 = scala.actors.Futures.awaitAll(5000, jobs: _*).count(_ != None)
|
||||
println("%d jobs in 5 seconds" format cnt1)
|
||||
println("%d jobs in 10 seconds" format cnt2)
|
||||
println("%d jobs in 15 seconds" format cnt3)
|
||||
}
|
||||
rate(List.fill(30)(() => task(scala.util.Random.nextInt(10)+1)))
|
||||
15
Task/Rate-counter/Scala/rate-counter-2.scala
Normal file
15
Task/Rate-counter/Scala/rate-counter-2.scala
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
def rate(n: Int, y: Int)(task: => Unit) {
|
||||
val startTime = System.currentTimeMillis
|
||||
var currTime = startTime
|
||||
var loops = 0
|
||||
do {
|
||||
task
|
||||
currTime = System.currentTimeMillis
|
||||
loops += 1
|
||||
} while (currTime - startTime < n * 1000 && loops < y)
|
||||
if (currTime - startTime > n * 1000)
|
||||
println("Rate %d times per %d seconds" format (loops - 1, n))
|
||||
else
|
||||
println("Rate %d times in %.3f seconds" format (y, (currTime - startTime).toDouble / 1000))
|
||||
}
|
||||
rate(5, 20)(task(2))
|
||||
5
Task/Rate-counter/Smalltalk/rate-counter.st
Normal file
5
Task/Rate-counter/Smalltalk/rate-counter.st
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
|times|
|
||||
times := Bag new.
|
||||
1 to: 10 do: [:n| times add:
|
||||
(Time millisecondsToRun: [3000 factorial])].
|
||||
Transcript show: times average asInteger.
|
||||
16
Task/Rate-counter/Tcl/rate-counter-1.tcl
Normal file
16
Task/Rate-counter/Tcl/rate-counter-1.tcl
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
set iters 10
|
||||
|
||||
# A silly example task
|
||||
proc theTask {} {
|
||||
for {set a 0} {$a < 100000} {incr a} {
|
||||
expr {$a**3+$a**2+$a+1}
|
||||
}
|
||||
}
|
||||
|
||||
# Measure the time taken $iters times
|
||||
for {set i 1} {$i <= $iters} {incr i} {
|
||||
set t [lindex [time {
|
||||
theTask
|
||||
}] 0]
|
||||
puts "task took $t microseconds on iteration $i"
|
||||
}
|
||||
1
Task/Rate-counter/Tcl/rate-counter-2.tcl
Normal file
1
Task/Rate-counter/Tcl/rate-counter-2.tcl
Normal file
|
|
@ -0,0 +1 @@
|
|||
puts [time { set aVar 123 } 1000000]
|
||||
5
Task/Rate-counter/UNIX-Shell/rate-counter-1.sh
Normal file
5
Task/Rate-counter/UNIX-Shell/rate-counter-1.sh
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
#!/bin/bash
|
||||
|
||||
while : ; do
|
||||
task && echo >> .fc
|
||||
done
|
||||
15
Task/Rate-counter/UNIX-Shell/rate-counter-2.sh
Normal file
15
Task/Rate-counter/UNIX-Shell/rate-counter-2.sh
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
./foo.sh &
|
||||
sleep 5
|
||||
mv .fc .fc2 2>/dev/null
|
||||
wc -l .fc2 2>/dev/null
|
||||
rm .fc2
|
||||
sleep 5
|
||||
mv .fc .fc2 2>/dev/null
|
||||
wc -l .fc2 2>/dev/null
|
||||
sleep 5
|
||||
mv .fc .fc2 2>/dev/null
|
||||
wc -l .fc2 2>/dev/null
|
||||
sleep 5
|
||||
killall foo.sh
|
||||
wc -l .fc 2>/dev/null
|
||||
rm .fc
|
||||
13
Task/Rate-counter/XPL0/rate-counter.xpl0
Normal file
13
Task/Rate-counter/XPL0/rate-counter.xpl0
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
include c:\cxpl\codes; \intrinsic 'code' declarations
|
||||
int N, I, T0, Time;
|
||||
[for N:= 1, 3 do
|
||||
[T0:= GetTime;
|
||||
for I:= 1 to 100 do
|
||||
[while port($3DA) & $08 do []; \wait for vertical retrace to go away
|
||||
repeat until port($3DA) & $08; \wait for vertical retrace signal
|
||||
];
|
||||
Time:= GetTime - T0;
|
||||
IntOut(0, Time); Text(0, " microseconds for 100 samples = ");
|
||||
RlOut(0, 100.0e6/float(Time)); Text(0, "Hz"); CrLf(0);
|
||||
];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue