Kevin Kelche
How to measure execution time in Go
To measure execution time in Golang we use time.Now()
and time.Since()
functions.
Output:
We can also use time.Now().Sub()
function to measure execution time.
Output:
We can also use time.Now().UnixNano()
function to measure execution time.
Output:
A better way to measure execution time of a function is to use the testing - package and perfom a benchmark test.
On running the tests we get the execution time of the function after running b.N
times which is the number of iterations.
Learn more about Benchmarking in this article.
-
Subscribe to my newsletter
Get the latest posts delivered right to your inbox.