How do I execute commands many many times per second in Golang?

Haoyuan Ge :

I want to print N lines per second.

for i := 0; i < N; i++ {
  fmt.Println(getLogs())
  time.Sleep(1000000000/N * time.Nanosecond)
}

But it seems fmt.Println() and getLogs() will also consume time. So actually it will cost me more than 1s to print N lines.

Say getLogs() and fmt.Println() will both cost 1 ms. And I want to print 1 million lines per second. Therefore, to print 1 line will cost 1 ms to getLogs(), 1 ms to print, and 1 ms to sleep... It will cost me 3s to print N lines.

Any better solutions to achieve this more accurately?

user6169399 :

You may use time.NewTicker (try The Go Playground):

package main

import (
    "fmt"
    "time"
)

func main() {
    const N = 4
    ticker := time.NewTicker(1000000000 / N * time.Nanosecond)
    for i := 0; i < N; i++ {
        fmt.Println(getLogs(i))
        <-ticker.C
    }
    ticker.Stop()
}

func getLogs(i int) int {
    time.Sleep(1 * time.Millisecond)
    return i
}

output:

0
1
2
3

See func NewTicker(d Duration) *Ticker Docs:

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.


Also see: Running code at noon in Golang

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling many methods of many objects many times per second

From Dev

If I don't know how many times something will run per second, how can I get the same output?

From Dev

How do I execute this find command multiple times throughout many directories with the ampersand '&' command?

From Dev

How can i execute a function many times while press a button?

From Javascript

Scroll event firing too many times. I only want it to fire a maximum of, say, once per second

From Dev

How do I make a transform Rotate n times per second?

From Dev

how many is too many Kafka commits per second

From Dev

How many synapses per node do I need?

From Dev

How do I get a function to repeat itself [x] many times?

From Dev

How many times do I need to run cdk bookstrap?

From Dev

How do you execute different options based on how many times a function is run?

From Java

Paint method execute many times

From Dev

cd to a directory and execute many commands

From Dev

How to Measure How Many Results App Can Produce Per Second

From Dev

Count how many times a certain value per user has changed

From Dev

How many times functions can be deployed per project?

From Dev

MQTT: How many times per minute MQTT client polls the server?

From Dev

Count how many times a word occurs in a HashMap per key

From Dev

Count how many times a value appears per month in dataframe

From Dev

How to check how many times UIButton pressed in One Second

From Dev

How many attempts per second can a password cracker actually make?

From Java

How many interrupts does my cpu have per second?

From

Is it possible to limit how many goroutines run per second?

From Dev

affdex-sdk. How many frames can processed per second?

From Dev

How to do a loop to execute many files

From Dev

Execute an action based on how many times an element appears in a list

From Dev

How many times does this statement in a triply-nested loop execute?

From Dev

How many times does the glsl fragment shader execute for one draw?

From Dev

Admob best practice: how many times do I request, and how long should I show a banner ad?

Related Related

  1. 1

    Calling many methods of many objects many times per second

  2. 2

    If I don't know how many times something will run per second, how can I get the same output?

  3. 3

    How do I execute this find command multiple times throughout many directories with the ampersand '&' command?

  4. 4

    How can i execute a function many times while press a button?

  5. 5

    Scroll event firing too many times. I only want it to fire a maximum of, say, once per second

  6. 6

    How do I make a transform Rotate n times per second?

  7. 7

    how many is too many Kafka commits per second

  8. 8

    How many synapses per node do I need?

  9. 9

    How do I get a function to repeat itself [x] many times?

  10. 10

    How many times do I need to run cdk bookstrap?

  11. 11

    How do you execute different options based on how many times a function is run?

  12. 12

    Paint method execute many times

  13. 13

    cd to a directory and execute many commands

  14. 14

    How to Measure How Many Results App Can Produce Per Second

  15. 15

    Count how many times a certain value per user has changed

  16. 16

    How many times functions can be deployed per project?

  17. 17

    MQTT: How many times per minute MQTT client polls the server?

  18. 18

    Count how many times a word occurs in a HashMap per key

  19. 19

    Count how many times a value appears per month in dataframe

  20. 20

    How to check how many times UIButton pressed in One Second

  21. 21

    How many attempts per second can a password cracker actually make?

  22. 22

    How many interrupts does my cpu have per second?

  23. 23

    Is it possible to limit how many goroutines run per second?

  24. 24

    affdex-sdk. How many frames can processed per second?

  25. 25

    How to do a loop to execute many files

  26. 26

    Execute an action based on how many times an element appears in a list

  27. 27

    How many times does this statement in a triply-nested loop execute?

  28. 28

    How many times does the glsl fragment shader execute for one draw?

  29. 29

    Admob best practice: how many times do I request, and how long should I show a banner ad?

HotTag

Archive