How long did the function run in Go (Golang)

How long did the function run in Go (Golang)

Go (Golang) is fast. This phrase is everywhere. It's always interesting how long it takes a function to finish its operation.

Here is an example of a helper function, that could be used to check and log the timing:

func TimeTrack(start time.Time, name string) {
?? ?
    elapsed := time.Since(start)
?? ?log.Printf("%s took %s", name, elapsed)

}        

It takes a start time and the name (could be a function name) as parameters.

Use it with the "defer" statement inside the functions:

func dosomething() {
?? ?
    defer TimeTrack(time.Now(), "dosomething Function")
?? ?
    time.Sleep(time.Millisecond * 1000)
?? ?fmt.Println("Printing inside dosomething")
}        

ps: ?a defer statement defers the execution of a function until the surrounding function returns.

Example with the main function:

func main() {
?? ?defer TimeTrack(time.Now(), "Main Function")
?? ?dosomething()
}        

As a result, the output would be:

Printing inside dosomething
2009/11/10 23:00:01 dosomething Function took 1s
2009/11/10 23:00:02 Main Function took 1s        

要查看或添加评论,请登录

Dmitry Golovach的更多文章

  • Swappable LLM Architectures: Building Flexible AI Systems

    Swappable LLM Architectures: Building Flexible AI Systems

    In today's fast-evolving AI landscape, new models emerge daily, pricing structures change, and performance improvements…

  • OpenWebUI - ChatGPT-Style Interface

    OpenWebUI - ChatGPT-Style Interface

    Following up on my previous post about getting phi3 running locally - while the command line interface is functional, a…

  • Running Local AI with Ollama

    Running Local AI with Ollama

    Why I Tried Running AI Locally I recently started running AI models on my own computer and it's been a game-changer…

  • Go meets Cisco using SSH

    Go meets Cisco using SSH

    I have already migrated some scripts from python to go, mostly with API. Decided to check how to SSH into network…

    1 条评论
  • WebApp: Cisco ISE and Python

    WebApp: Cisco ISE and Python

    My previous post “Python and ISE Monitor Mode” was about how to collect access-session information from the switch and…

    9 条评论
  • Python and ISE Monitor Mode

    Python and ISE Monitor Mode

    There are several ways to run ISE (wired) in monitor mode and AuthZ results: dACL, another VLAN, etc. It is always a…

    9 条评论
  • Python: Classes and Methods

    Python: Classes and Methods

    The simplest class in Python: Class and Instance Attributes Class attributes are attributes that are owned by the class…

    1 条评论
  • Python Dictionaries and JSON

    Python Dictionaries and JSON

    Dictionaries Dictionary – a collection of keys and values, unordered, changeable and indexed. >>> my_dict = {1: 'one'…

    5 条评论
  • Python: Apply config to multiple interfaces (with the condition)

    Python: Apply config to multiple interfaces (with the condition)

    It is not about range feature:) After my post about how to get into the switch with “not sure” credentials, let’s…

    2 条评论
  • Python More About Lists

    Python More About Lists

    List Comprehensions One way to create a list with for loop: >>> new_list = [] >>> old_list = [1, 2, 3, 4] >>> for each…

    1 条评论

社区洞察

其他会员也浏览了