-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtinytime.pas
62 lines (50 loc) · 1023 Bytes
/
dtinytime.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
unit dtinytime;
// Tiny timing utilities. rlyeh, public domain | wtrmrkrlyeh
// ported to pascal by Doj
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
interface
type
TProcedure = procedure;
function now: Double;
function bench(fn: TProcedure): Double;
procedure sleep(secs: Double);
implementation
uses
SysUtils,
DateUtils;
function now: Double;
var
Timestamp: TTimeStamp;
begin
TimeStamp := DateTimeToTimeStamp(SysUtils.Now);
Exit(TimeStamp.Date * 24*60*60 + TimeStamp.Time / 1000);
end;
function bench(fn: TProcedure): Double;
var
start: TDateTime;
begin
start := SysUtils.Now;
fn;
Exit(DateUtils.MilliSecondsBetween(start, SysUtils.Now) / 1000);
end;
procedure sleep(secs: Double);
begin
SysUtils.Sleep(Trunc(secs * 1000));
end;
// procedure example;
// begin
// sleep(0.1234);
// end;
//
// var
// t0, t1: Double;
// begin
// t0 := now;
// Writeln(bench(@example):0:6, ' s.');
// t1 := now;
// Writeln(t1 - t0 :0:6, ' s.');
// end.
end.