Nitro
Nitro

Reputation: 73

golang execute function at exactly every 1 millisecond over 1 second ( 1000 call per second)

I am new to golang.

I want to call a function at exactly 1 milliseconds for a certain duration ( 1 second for now ) , the function should be called 1000 time a seconds .

I did follow and implemented multiple approaches using timer and tick , but when testing on a simple function that just increment by one a value every time it's been called I find out that it's only been called about ~630 time in 1 second .

When I call the function in loop and stop it after 1 second , The function is called about ~ 226370427 time in 1 second , which means I have problem with the timer and tick .

Here is my code that I implemented using goroutine concurrency which keeps calling the function that increment every 1 ms using ticker and stopped after 1 second by the main and the sum value is then read using the int channel.

import (
    "fmt"
    "time"
)

func main() {
    tickChan := time.NewTicker(time.Millisecond * 1).C

    doneChan := make(chan bool)
    resultChan := make(chan int)
    go start(tickChan, doneChan, resultChan)
    time.Sleep(time.Second * 1)
    doneChan <- true // Set it to true to stop the infinte loop in the routine
    fmt.Printf("total is : %v\n", <-resultChan)

}

func start(tickChan <-chan time.Time, doneChan <-chan bool, resultChan chan int) {
    sum := 0 // <-- The the value to increment
    for {
        select {
        case <-tickChan:
            sum++
        case <-doneChan:
            resultChan <- sum
            return
        }
    }
}

I was able to do it in node.js successfully (using nanotimer lib ) and was able to run exactly 1000 in 1 sec for every 1 ms , I was even to do it in microsecond too (tho it was about ~ 988676 in 1 second instead of 1 million )

Edit :

Here is my go version go version go1.10.2 windows/amd64

and go env `

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\name\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\name\Documents\Go Projects
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\name\AppData\Local\Temp\go-build403228958=/tmp/go-build -gno-record-gcc-switches

`

EDIT 2 : I tested it on the playground and manged to get 1000 total , I think the issue is with windows .Any way to fix it ?

Upvotes: 4

Views: 9310

Answers (2)

JeffW
JeffW

Reputation: 61

There is a way to do this, via CGo.

You would have to configure a '/dev/rtc1' resource, and set it to the desired frequency. Then, use the 'read()' C library call to get a 1 millisecond pause. Only 1, administrator, user can execute this.

This is platform-specific.

In Windows, you'll need to find another way to access the clock chip to do this.

Upvotes: 0

peterSO
peterSO

Reputation: 166825

Package time

import "time"

type Ticker

A Ticker holds a channel that delivers `ticks' of a clock at intervals.

type Ticker struct {
        C <-chan Time // The channel on which the ticks are delivered.
        // contains filtered or unexported fields
}

func NewTicker

func NewTicker(d Duration) *Ticker

NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument. It adjusts the intervals or drops ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.


Here are my results for Windows 10.0.17134 and Linux 4.15 from the same dual-booted machine.

package main

import (
    "fmt"
    "runtime"
    "time"
)

func ticker(tick time.Duration) {
    ticker := time.NewTicker(tick)
    defer ticker.Stop()
    done := make(chan bool)
    sleep := 1 * time.Second
    go func() {
        time.Sleep(sleep)
        done <- true
    }()
    ticks := 0
    for {
        select {
        case <-done:
            fmt.Printf("%v × %v ticks in %v\n", ticks, tick, sleep)
            return
        case <-ticker.C:
            ticks++
        }
    }
}

func main() {
    fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))
    start := time.Duration(1 * time.Millisecond)
    end := start / 1000
    for tick := start; tick >= end; tick /= 10 {
        ticker(tick)
    }
}

Output:

Microsoft Windows [Version 10.0.17134.112]:

GOMAXPROCS: 4
554 × 1ms ticks in 1s
552 × 100µs ticks in 1s
542 × 10µs ticks in 1s
552 × 1µs ticks in 1s

Linux 4.15.0-23-generic:

GOMAXPROCS: 4
1000 × 1ms ticks in 1s
9990 × 100µs ticks in 1s
13169 × 10µs ticks in 1s
14014 × 1µs ticks in 1s

Sleep function | Microsoft Docs

Suspends the execution of the current thread until the time-out interval elapses.

Parameters

dwMilliseconds

The time interval for which execution is to be suspended, in milliseconds.


On Windows, the Go Ticker resolution is likely limited to, at best, a millisecond.

Upvotes: 2

Related Questions