Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: Nanosecond() on Windows return identical value in loop execution #28084

Closed
wenchunzhang opened this issue Oct 8, 2018 · 5 comments

Comments

Projects
None yet
5 participants
@wenchunzhang
Copy link

commented Oct 8, 2018

What version of Go are you using (go version)?

1.9.4

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

Build Windows executable on Mac, and run the executable on Windows

What did you do?

func main() {

	t := os.Getenv("IN_TIMES")
	count, err := strconv.Atoi(t)
	if err != nil {
		count = 10
	}

	for i := 0; i < count; i++ {
		fmt.Printf("current time nano: %s \n", strconv.Itoa(time.Now().Nanosecond()))
	}
}

What did you expect to see?

Except to see different output nasosecond value

What did you see instead?

See identical output:

current time nano: 9116500
current time nano: 9116500
current time nano: 9116500
current time nano: 9116500
current time nano: 9116500
current time nano: 9116500
current time nano: 9116500
current time nano: 9116500
current time nano: 9116500
current time nano: 9116500

@wenchunzhang wenchunzhang changed the title time: Nanosecond() on Windows return same value in loop execution time: Nanosecond() on Windows return identical value in loop execution Oct 8, 2018

@agnivade agnivade added this to the Go1.12 milestone Oct 9, 2018

@kardianos

This comment has been minimized.

Copy link
Contributor

commented Oct 9, 2018

You may want to try it with go1.11, but by default the platform timer is lower resolution then other system.

I can't remember if Alex was successful in integrating the high res timers or if that turned out to be too expensive.

@mattn

This comment has been minimized.

Copy link
Member

commented Oct 9, 2018

I tried with IN_TIMES=100

current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 860335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 861335000 
current time nano: 862335000 

The resolution of timer on Windows is not good. And Go's runtime adjusts this resolution internally so you probably can not get a fine resolution. (AFAIK)

@agnivade

This comment has been minimized.

Copy link
Member

commented Oct 9, 2018

Seems like that. @alexbrainman for further comments.

We can close this out if this is WAI.

@alexbrainman

This comment has been minimized.

Copy link
Member

commented Oct 9, 2018

Yes, what everyone else said. Windows clock does not provides resolution in nanoseconds.

Alex

@agnivade

This comment has been minimized.

Copy link
Member

commented Oct 9, 2018

Seems like WAI. Closing.

@agnivade agnivade closed this Oct 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.