Logo

Programming-Idioms

  • Go
  • Python

Idiom #125 Measure function call duration

measure the duration t, in nanoseconds, of a call to the function foo. Print this duration.

import time
t1 = time.perf_counter_ns()
foo()
t2 = time.perf_counter_ns()
print('Nanoseconds:', t2 - t1)

t1 and t2 are int
import "time"
t1 := time.Now()
foo()
t := time.Since(t1)
ns := int64(t / time.Nanosecond)
fmt.Printf("%dns\n", ns)

t1 has type time.Time.
t has type time.Duration.
import "time"
t1 := time.Now()
foo()
t := time.Since(t1)
ns := t.Nanoseconds()
fmt.Printf("%dns\n", ns)

t1 has type time.Time.
t has type time.Duration.
ns has type int64.
(time (foo))

time prints time elapsed, but in ms

New implementation...
< >
JPSII