Which one of the two is idiomatic way? time.Sleep() or ticker?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.



time.Sleep()



func main() 

go func()
for
time.Sleep(time.Minute)
fmt.Println("Hi")

()

time.Sleep(10 * time.Minute) //just to keep main thread running




Or Ticker



func main() 

go func()
for _ = range time.Tick(time.Minute)
fmt.Println("Hi")


()

time.Sleep(10 * time.Minute) //just to keep main thread running











share|improve this question






























    1















    I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.



    time.Sleep()



    func main() 

    go func()
    for
    time.Sleep(time.Minute)
    fmt.Println("Hi")

    ()

    time.Sleep(10 * time.Minute) //just to keep main thread running




    Or Ticker



    func main() 

    go func()
    for _ = range time.Tick(time.Minute)
    fmt.Println("Hi")


    ()

    time.Sleep(10 * time.Minute) //just to keep main thread running











    share|improve this question


























      1












      1








      1








      I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.



      time.Sleep()



      func main() 

      go func()
      for
      time.Sleep(time.Minute)
      fmt.Println("Hi")

      ()

      time.Sleep(10 * time.Minute) //just to keep main thread running




      Or Ticker



      func main() 

      go func()
      for _ = range time.Tick(time.Minute)
      fmt.Println("Hi")


      ()

      time.Sleep(10 * time.Minute) //just to keep main thread running











      share|improve this question
















      I have to execute some statements say every minute. I am not sure which one of the following I should follow. It would be great if someone can explain the pros and cons in terms of memory and CPU.



      time.Sleep()



      func main() 

      go func()
      for
      time.Sleep(time.Minute)
      fmt.Println("Hi")

      ()

      time.Sleep(10 * time.Minute) //just to keep main thread running




      Or Ticker



      func main() 

      go func()
      for _ = range time.Tick(time.Minute)
      fmt.Println("Hi")


      ()

      time.Sleep(10 * time.Minute) //just to keep main thread running








      go






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 9:09









      Sagar Zala

      2,41151438




      2,41151438










      asked Nov 15 '18 at 9:01









      drjoenhdrjoenh

      112




      112






















          2 Answers
          2






          active

          oldest

          votes


















          5














          From the 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.




          time.Sleep just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.



          The ticker takes the execution time of the provided block into account and skips an interval, if necessary.



          Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.



          In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.



          In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.






          share|improve this answer

























          • To me it's also significant that the Ticker code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.

            – Adrian
            Nov 15 '18 at 14:12


















          0














          In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315716%2fwhich-one-of-the-two-is-idiomatic-way-time-sleep-or-ticker%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            From the 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.




            time.Sleep just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.



            The ticker takes the execution time of the provided block into account and skips an interval, if necessary.



            Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.



            In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.



            In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.






            share|improve this answer

























            • To me it's also significant that the Ticker code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.

              – Adrian
              Nov 15 '18 at 14:12















            5














            From the 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.




            time.Sleep just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.



            The ticker takes the execution time of the provided block into account and skips an interval, if necessary.



            Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.



            In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.



            In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.






            share|improve this answer

























            • To me it's also significant that the Ticker code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.

              – Adrian
              Nov 15 '18 at 14:12













            5












            5








            5







            From the 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.




            time.Sleep just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.



            The ticker takes the execution time of the provided block into account and skips an interval, if necessary.



            Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.



            In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.



            In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.






            share|improve this answer















            From the 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.




            time.Sleep just waits for the provided time and continues with the program. There is no adjustment if the rest of the code takes longer.



            The ticker takes the execution time of the provided block into account and skips an interval, if necessary.



            Imagine this scenario: You provide an interval of one minute and your code takes 10 seconds to execute.



            In your first version your program executes your code for ten seconds and then sleeps for 60 seconds. Practically it gets called every 70 seconds.



            In your second version your code gets executed for 10 seconds, then the ticker adjusts the wait time to 50 seconds. Your code gets executed exactly each minute.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 14:10

























            answered Nov 15 '18 at 9:05









            mbuechmannmbuechmann

            3,07121429




            3,07121429












            • To me it's also significant that the Ticker code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.

              – Adrian
              Nov 15 '18 at 14:12

















            • To me it's also significant that the Ticker code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.

              – Adrian
              Nov 15 '18 at 14:12
















            To me it's also significant that the Ticker code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.

            – Adrian
            Nov 15 '18 at 14:12





            To me it's also significant that the Ticker code is simpler and clearer; it sets up a block of code and indicates it is to be run every (duration). The Sleep code, on the other hand, is less concise and less obvious: it sets up an infinite loop, then at the very end adds a sleep.

            – Adrian
            Nov 15 '18 at 14:12













            0














            In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.






            share|improve this answer



























              0














              In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.






              share|improve this answer

























                0












                0








                0







                In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.






                share|improve this answer













                In the example above there is no difference. But ticker also concurrent safe since it uses channels. For example you can create few workers that should make some job every 2 seconds and you need 1 worker make this work at same time, than you use ticker.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 9:09









                MagiqMagiq

                213




                213



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315716%2fwhich-one-of-the-two-is-idiomatic-way-time-sleep-or-ticker%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo