In python I can see how many seconds have elapsed during a specific process like,
started = time.time()
doProcess()
print(time.time()-started)
Whats the equivelent in golang?
import (
"fmt"
"time"
)
func main() {
begin := time.Now()
time.Sleep(10 * time.Millisecond)
end := time.Now()
duration := end.Sub(begin)
fmt.Println(duration)
}
import (
"fmt"
"time"
)
func main() {
started := time.Now()
doProcess()
fmt.Println(time.Now().Sub(started).Seconds())
}
Package time
func Since
func Since(t Time) Duration
Since returns the time elapsed since t. It is shorthand for
time.Now().Sub(t).
Your Python example in Go:
package main
import (
"fmt"
"time"
)
func main() {
started := time.Now()
time.Sleep(1 * time.Second)
fmt.Println(time.Since(started))
}
Output:
1s
Related
how do we convert time.now() in time.Time( RFC3339) format?
Eg:
var t time.Time
timeNow= time.Now()
I want to assign timeNow to t
Golang has support for various time formats.
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
r := t.Format(time.RFC3339)
fmt.Println("time.Now() ", t)
fmt.Println("RFC3339 ", r)
}
Trying to obtain successive timestamps with below Go code
package main
import "fmt"
import "time"
func main () {
ts := int64(1500000000) // start
for i := int64(0); i<1e6; i = i + 1000 { // successively add 1e3
t := time.Unix(ts, i) // Get start + i
fmt.Printf("%d %02d:%02d\n", ts+i, t.Hour(), t.Minute())
}
}
, output remains like
....
1500995000 04:40
1500996000 04:40
1500997000 04:40
1500998000 04:40
1500999000 04:40
Please, what is the problem here ? Why the hour:minute doesn't vary ? (04:40)
go version go1.15.6 linux/amd64
You are changing the nanoseconds and printing minutes...
https://pkg.go.dev/time#go1.16.7#Unix
func Unix(sec int64, nsec int64) Time
Check this:
https://play.golang.org/p/_Ywn9S5Khch
package main
import "fmt"
import "time"
func main () {
ts := int64(1500000000) // start
for i := int64(0); i<1e6; i = i + 1000 { // successively add 1e3
t := time.Unix(ts, i) // Get start + i
fmt.Printf("%d %02d:%02d ---- %2d\n", ts+i, t.Hour(), t.Minute(), t.UnixNano())
}
}
I am attempting to clock the execution of a process and I need the value in seconds.
package main
import (
"fmt"
"time"
)
func main() {
startTime := time.Now()
time.Sleep(1379 * time.Millisecond)
elapsedTime := time.Since(startTime)
fmt.Println(elapsedTime) //->1.379s
secs := float64(elapsedTime / time.Second)
fmt.Println(secs)//->1
//desired output is: 1.379
}
I am looking for a way to have the time not NOT being rounded.
Here's the playground: https://play.golang.org/p/VLgKTpmkHPS
Just use the Seconds() method:
package main
import (
"fmt"
"time"
)
func main() {
elapsedTime := 1379 * time.Millisecond
fmt.Println(elapsedTime) //->1.379s
secs := elapsedTime.Seconds()
fmt.Println(secs)
}
playground.
Epoc time to to convert the time to seconds
update : Below code works ?
package main
import (
"fmt"
"time"
)
func main() {
startTime := time.Now()
nanos := startTime.Unix()
// fmt.Println(startTime)
millis := nanos / 1000000
elapsedInsecondsT := float64(millis)/float64(1000)
fmt.Println( millis, elapsedInsecondsT )
}
courtesy : https://gobyexample.com/epoch
Is there a way to scan a big.Int directly from the standard input in Go? Right now I'm doing this:
package main
import (
"fmt"
"math/big"
)
func main() {
w := new(big.Int)
var s string
fmt.Scan(&s)
fmt.Sscan(s, w)
fmt.Println(w)
}
I also could have used .SetString. But, is there a way to Scan the big.Int directly from the standard input without scanning a string or an integer first?
For example,
package main
import (
"fmt"
"math/big"
)
func main() {
w := new(big.Int)
n, err := fmt.Scan(w)
fmt.Println(n, err)
fmt.Println(w.String())
}
Input (stdin):
295147905179352825857
Output (stdout):
1 <nil>
295147905179352825857
As far as I know - no, there's no other way. In fact, what you've got is the default example they have for scanning big.Int in the documentation.
package main
import (
"fmt"
"log"
"math/big"
)
func main() {
// The Scan function is rarely used directly;
// the fmt package recognizes it as an implementation of fmt.Scanner.
i := new(big.Int)
_, err := fmt.Sscan("18446744073709551617", i)
if err != nil {
log.Println("error scanning value:", err)
} else {
fmt.Println(i)
}
}
You can see the relevant section here - http://golang.org/pkg/math/big/#Int.Scan
This example taken from tour.golang.org/#63
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
The output
hello
world
hello
world
hello
world
hello
world
hello
Why world is printed only 4 times instead of 5 ?
Edit: The answer can be quoted from golang specification:
Program execution begins by initializing the main package and then
invoking the function main. When the function main returns, the
program exits. It does not wait for other (non-main) goroutines to
complete.
When your main function ends your program ends, i.e. all goroutines are terminated.
Your main terminates before go say("world") is done. If you sleep some time at the end of main you should see the last world.
Here is how you solve that synchronization problem properly - with sync.WaitGroup
Playground link
package main
import (
"fmt"
"sync"
"time"
)
func say(s string, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
wg := new(sync.WaitGroup)
wg.Add(2)
go say("world", wg)
go say("hello", wg)
wg.Wait()
fmt.Println("All done")
}
Because the calling gorouting terminates before the second one you spawned does. This causes the second to shut down. To illustrate, modify your code slightly:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Print(i)
fmt.Println(":"+s)
}
}
func main() {
go say("world")
say("hello")
}
Try putting in a "wait" or a sleep to the end of the main function.