首页 > 其他分享 >Go - Measuring Lapsed Time

Go - Measuring Lapsed Time

时间:2023-10-06 12:33:50浏览次数:35  
标签:Measuring clock Time t2 monotonic t1 time Go

Problem: You want to measure the lapsed time and make sure that it is accurate.


Solution: Use the monotonic clock in the Time struct to find the lapsed time.

 

The Time struct contains the data but only under certain circumstances. If you create a Time struct instance using time.Now , and print out the struct, you will be able to see the monotonic clock:

t   :=   time . Now () 
fmt . Println ( t )

When you run it you see this:

2021 - 10 - 09  13:10:43.311791  +0800  +08  m=+0.000093742

The m=+0.000093742 part is the monotonic clock. The part before that is the wall clock. This is what the package documentation says:
If the time has a monotonic clock reading, the returned string includes a final field “m=±<value>”, where value is the monotonic clock reading formatted as a decimal number of seconds.
But what does it mean by the monotonic clock reading formatted as a decimal number of seconds ? It’s a really small number! Actually, this just shows how long your program has been running.

func   main ()   { 
      time . Sleep ( 10   *   time . Second )   //  pretend  to  do  something  for  10s 
      t1   :=   time . Now () 
      t2   :=   time . Now () 
      fmt . Println ( "t1:" ,   t1 ) 
      fmt . Println ( "t2:" ,   t2 ) 
      fmt . Println ( "difference:" ,   t2 . Sub ( t1 )) 
}

This is what you should see after 10 seconds:

t1:  2021 - 10 - 09  15:12:12.432516  +0800  +08  m=+10.005330678
t2:  2021 - 10 - 09  15:12:12.432516  +0800  +08  m=+10.005330984
difference:  306ns

The interesting point to note here is that if you use the wall clock, you won’t be able to tell the difference at all!

As noted earlier, the monotonic clock data is not always available in the Time struct. Methods such as AddDate , Round , and Truncate are wall clock computations so the Time structs they return won’t have the monotonic clock. Similarly, In , Local , and UTC interpret the wall clock, so the Time structs they return won’t have the monotonic clock either.

You can also remove the monotonic clock yourself. Just use the Round method with a 0 parameter:

t   :=   time . Now (). Round ( 0 ) 
fmt . Println ( t )

You should get something like this:

2021 - 10 - 09  15:25:31.369518  +0800  +08

You no longer see the monotonic clock here. So what happens when you try to call Sub (or any other monotonic methods) on a Time struct instance that doesn’t have the monotonic clock?

func   main ()   { 
      t1   :=   time . Now (). Round ( 0 ) 
      t2   :=   time . Now (). Round ( 0 ) 
      fmt . Println ( "t1:" ,   t1 ) 
      fmt . Println ( "t2:" ,   t2 ) 
      fmt . Println ( "difference:" ,   t2 . Sub ( t1 )) 
}

Now t1 and t2 have their monotonic clocks stripped away. If you try to find the difference between these two times, you will get a big fat 0. This is because the operation will be done on the wall clock instead of the monotonic clock, and the wall clock just isn’t fine - grained enough:

t1:  2021 - 10 - 09  15:28:38.451622  +0800  +08
t2:  2021 - 10 - 09  15:28:38.451622  +0800  +08
difference:  0s

 

标签:Measuring,clock,Time,t2,monotonic,t1,time,Go
From: https://www.cnblogs.com/zhangzhihui/p/17744428.html

相关文章

  • go通过pprof定位groutine泄漏
    日常开发中除了会出现Panic、ErrorInfo等通过日志很容易捕捉到的错误,还会出现内存泄漏、CPU激增等日志难以捕捉的问题。今天小老虎就给大家介绍下如何使用pprof去捕捉内存、CPU这些日志难以排查到的问题。pprof的访问pprof是Golang的性能分析工具,可以帮助我们查看程序在运行过程中C......
  • 基于Django的智慧旅游系统的设计与实现-计算机毕业设计源码+LW文档
    摘 要在各学校的教学过程中,智慧旅游系统是一项非常重要的事情。随着计算机多媒体技术的发展和网络的普及。采用当前流行的B/S模式以及3层架构的设计思想通过Python技术来开发此系统的目的是建立一个配合网络环境的智慧旅游系统,这样可以有效地解决智慧旅游管理信息混乱的局面。......
  • go语言ent教程:使用zerolog定制ent日志
    背景:ent开始debug模式后,可以输出日志,但是我们想为ent接入zerolog,该怎么做呢? 一、引入zerologgoget-ugithub.com/rs/zerolog 二、自定义zerolog配置customLog:=func(args...any){str:=fmt.Sprintf("%v",args)fmt.Println(str)......
  • go语言ent教程:开启debug调试模式
    背景:ent模式是没有开启debug模式的 开启ent的调试模式有2种方法:一、通过配置选项开启client,err:=ent.Open("mysql","root:dev@123456@tcp(localhost:3306)/test?parseTime=True",ent.Debug()) 二、客户端调用Debug函数iferr:=client.Schema.Crea......
  • 《制作Docker镜像》——以Django镜像为例
    一、建一个基础的Ubuntu环境执行以下指令,docker就会搞一个ubuntu环境,如果你没有嘞?它就会自己下载的。dockerrun-itubtuntu然后你就会发现,自己进入了镜像了。二、给容器换源那么这个我们就只需要去清华的镜像。然后选择自己对应的版本。【--->Ubuntu软件仓库<---】接......
  • Go - Representing Duration
    Problem: Youwanttospecifyadurationoftime.Solution: UsetheDurationtypetorepresentaspanoftime. Themainrepresentationforaspanoftimeinthetimepackageis,ofcourse,Duration.Durationisnothingfancy,though;it’sjustanint64......
  • 将np.datetime64的时间差转为秒
    print((np.datetime64('2023-01-0200:00:00')-np.datetime64('2023-01-0100:00:00'))/np.timedelta64(1,'s'),(pd.Series(np.datetime64('2023-01-0200:00:00'))-pd.Series(np.datetime64('2023-01-0100:0......
  • C++ Profiler Introduction [CPU Time Only]
    C++ProfilerIntroduction[CPUTimeOnly]author:LastWhisperdate:2023/10/05ThereareseveralprofilersforC++.Basedonmyresearch,I'vefoundthattracyisthemostpowerful.However,it'schallengingtoconfigure.Toquicklybenchmark......
  • MongoDB高阶特性:事务、索引
    一、事务一)MongoDB的事务首先我们需要知道MongoDB是有多种存储引擎的,不同的存储引擎在实现ACID的时候,使用不同的机制。而Mongodb从3.0开始默认使用的是WiredTiger引擎,本文后续所有文字均是针对WiredTiger引擎。WiredTiger引擎可以针对单个文档来保证ACID特性,但是当需要操作多个......
  • Go - Representing Time Zones
    Problem: YouwanttoincludethetimezoneinformationinaTimestruct.Solution: TheTimestructincludesaLocation,whichistherepresentationofthetimezone. Atimezoneisanareathatfollowsastandardtimethatroughlyfollowslongitudebu......