a mini social media app for small communities
1module util
2
3import time
4
5@[noinit]
6pub struct Stopwatch {
7pub:
8 start time.Time = time.now()
9pub mut:
10 stop time.Time
11 took ?time.Duration
12}
13
14@[inline]
15pub fn Stopwatch.new() Stopwatch {
16 return Stopwatch{}
17}
18
19@[inline]
20pub fn (mut stopwatch Stopwatch) stop() time.Duration {
21 stopwatch.stop = time.now()
22 duration := stopwatch.stop - stopwatch.start
23 stopwatch.took = duration
24 return duration
25}
26
27@[params]
28pub struct TimeItParams {
29pub:
30 it fn () @[required]
31 name string
32 log bool
33}
34
35@[inline]
36pub fn time_it(params TimeItParams) Stopwatch {
37 mut stopwatch := Stopwatch.new()
38 params.it()
39 took := stopwatch.stop()
40 if params.log {
41 println('-> (time_it) ${params.name} took ${took}')
42 }
43 return stopwatch
44}