Skip to content

Commit fcf346c

Browse files
committed
add timer package as copy of emergent one, to avoid circular dependency; update go.mod
1 parent fa5c57c commit fcf346c

File tree

6 files changed

+95
-18
lines changed

6 files changed

+95
-18
lines changed

examples/axon/main.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313

1414
"cogentcore.org/core/math32"
1515
"cogentcore.org/core/vgpu"
16-
"github.com/emer/emergent/v2/timer"
1716
"github.com/emer/gosl/v2/sltype"
1817
"github.com/emer/gosl/v2/threading"
18+
"github.com/emer/gosl/v2/timer"
1919
)
2020

2121
// DiffTol is tolerance on testing diff between cpu and gpu values
@@ -114,7 +114,7 @@ func main() {
114114
// fmt.Printf("%d\ttime.RandCtr: %v\n", cy, time.RandCtr.Uint2())
115115
}
116116

117-
cpuTmr.Stop()
117+
// cpuTmr.Stop()
118118

119119
time.Reset()
120120

@@ -145,9 +145,6 @@ func main() {
145145
// seti.ConfigValues(1) // one val per var
146146
sy.Config() // configures vars, allocates vals, configs pipelines..
147147

148-
gpuFullTmr := timer.Time{}
149-
gpuFullTmr.Start()
150-
151148
// this copy is pretty fast -- most of time is below
152149
lvl, _ := layv.Values.ValueByIndexTry(0)
153150
lvl.CopyFromBytes(unsafe.Pointer(&lays[0]))
@@ -158,9 +155,6 @@ func main() {
158155
// ivl, _ := idxv.Values.ValueByIndexTry(0)
159156
// ivl.CopyFromBytes(unsafe.Pointer(&idxs[0]))
160157

161-
// gpuFullTmr := timer.Time{}
162-
// gpuFullTmr.Start()
163-
164158
sy.Mem.SyncToGPU()
165159

166160
vars.BindDynamicValueIndex(0, "Layers", 0)
@@ -171,8 +165,8 @@ func main() {
171165
cmd := sy.ComputeCmdBuff()
172166
sy.CmdResetBindVars(cmd, 0)
173167

174-
// gpuFullTmr := timer.Time{}
175-
// gpuFullTmr.Start()
168+
gpuFullTmr := timer.Time{}
169+
gpuFullTmr.Start()
176170

177171
gpuTmr := timer.Time{}
178172
gpuTmr.Start()

examples/basic/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
"cogentcore.org/core/math32"
1414
"cogentcore.org/core/vgpu"
15-
"github.com/emer/emergent/v2/timer"
15+
"github.com/emer/gosl/v2/timer"
1616
)
1717

1818
// note: standard one to use is plain "gosl" which should be go install'd

examples/rand/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
"cogentcore.org/core/math32"
1515
"cogentcore.org/core/vgpu"
16-
"github.com/emer/emergent/v2/timer"
1716
"github.com/emer/gosl/v2/sltype"
17+
"github.com/emer/gosl/v2/timer"
1818
)
1919

2020
// note: standard one to use is plain "gosl" which should be go install'd

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ module github.com/emer/gosl/v2
33
go 1.22
44

55
require (
6-
cogentcore.org/core v0.0.9
7-
github.com/emer/emergent/v2 v2.0.0-dev0.0.11
6+
cogentcore.org/core v0.1.0
87
golang.org/x/tools v0.19.0
98
)
109

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
cogentcore.org/core v0.0.9 h1:Ird5o4wewSe8/dvsmd47YvkP5JZCiaE9hUSrb2692N8=
2-
cogentcore.org/core v0.0.9/go.mod h1:SAJedgd5nd0iB2cHl//DyCO+rugd/u7KCHXHNjnQOmA=
1+
cogentcore.org/core v0.1.0 h1:fJjpk7jd77IIUCDoauwukOeTVsWU+SuDyIhDXb3PYTM=
2+
cogentcore.org/core v0.1.0/go.mod h1:SAJedgd5nd0iB2cHl//DyCO+rugd/u7KCHXHNjnQOmA=
33
github.com/Bios-Marcel/wastebasket v0.0.4-0.20240213135800-f26f1ae0a7c4 h1:6lx9xzJAhdjq0LvVfbITeC3IH9Fzvo1aBahyPu2FuG8=
44
github.com/Bios-Marcel/wastebasket v0.0.4-0.20240213135800-f26f1ae0a7c4/go.mod h1:FChzXi1izqzdPb6BiNZmcZLGyTYiT61iGx9Rxx9GNeI=
55
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
@@ -21,8 +21,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
2121
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2222
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
2323
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24-
github.com/emer/emergent/v2 v2.0.0-dev0.0.11 h1:c0gTXdE5SUl590TjhFPXykCyhFe+jMlH4aegFrrqa4g=
25-
github.com/emer/emergent/v2 v2.0.0-dev0.0.11/go.mod h1:i+OqOi5bam3Nl6QZxwp5QSA2djUZ1+y9e+Gtixv6bFc=
2624
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
2725
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
2826
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=

timer/timer.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2019, The Emergent Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Package timer provides a simple wall-clock duration timer based on standard
6+
// time. Accumulates total and average over multiple Start / Stop intervals.
7+
// This is copied from emergent/timer, to avoid circular dependency.
8+
package timer
9+
10+
//go:generate core generate -add-types
11+
12+
import "time"
13+
14+
// Time manages the timer accumulated time and count
15+
type Time struct {
16+
17+
// the most recent starting time
18+
St time.Time
19+
20+
// the total accumulated time
21+
Total time.Duration
22+
23+
// the number of start/stops
24+
N int
25+
}
26+
27+
// Reset resets the overall accumulated Total and N counters and start time to zero
28+
func (t *Time) Reset() {
29+
t.St = time.Time{}
30+
t.Total = 0
31+
t.N = 0
32+
}
33+
34+
// Start starts the timer
35+
func (t *Time) Start() {
36+
t.St = time.Now()
37+
}
38+
39+
// ResetStart reset then start the timer
40+
func (t *Time) ResetStart() {
41+
t.Reset()
42+
t.Start()
43+
}
44+
45+
// Stop stops the timer and accumulates the latest start - stop interval, and also returns it
46+
func (t *Time) Stop() time.Duration {
47+
if t.St.IsZero() {
48+
t.Total = 0
49+
t.N = 0
50+
return 0
51+
}
52+
iv := time.Now().Sub(t.St)
53+
t.Total += iv
54+
t.N++
55+
return iv
56+
}
57+
58+
// Avg returns the average start / stop interval (assumes each was measuring the same thing).
59+
func (t *Time) Avg() time.Duration {
60+
if t.N == 0 {
61+
return 0
62+
}
63+
return t.Total / time.Duration(t.N)
64+
}
65+
66+
// AvgSecs returns the average start / stop interval (assumes each was measuring the same thing)
67+
// as a float64 of seconds
68+
func (t *Time) AvgSecs() float64 {
69+
if t.N == 0 {
70+
return 0
71+
}
72+
return float64(t.Total) / (float64(t.N) * float64(time.Second))
73+
}
74+
75+
// AvgMSecs returns the average start / stop interval as a float64 of milliseconds
76+
func (t *Time) AvgMSecs() float64 {
77+
if t.N == 0 {
78+
return 0
79+
}
80+
return float64(t.Total) / (float64(t.N) * float64(time.Millisecond))
81+
}
82+
83+
// TotalSecs returns the total start / stop intervals as a float64 of seconds.
84+
func (t *Time) TotalSecs() float64 {
85+
return float64(t.Total) / float64(time.Second)
86+
}

0 commit comments

Comments
 (0)