DEV Community

Thamara Andrade
Thamara Andrade

Posted on • Originally published at

How to sleep... in different programming languages

Every now and then, as a developer, we need to add a sleep in our code. Usually to make some test work, or god knows the reason. In particular, I use this a lot in Tcl, as this is the primary language for the tests I write at work. And there was not a single time where I tried a simple "sleep" command, only to have it fail. The reason? In Tcl, the sleep command is actually "after", something I find exceptionally not natural.

To prove my point on the wrong name for such command in Tcl, I decided to investigate how the same is achieved in different languages. In all examples below, I'll be implementing a sleep of 3 seconds and judging the language on the way it allows the sleep to be implemented. The languages I choose are based on the most popular programming languages, which I found on some random site, and I cannot guarantee the veracity.

Please note that I'm not proficient in many of the languages I mention here. Feel free to contact me on @thamyk if you found some error, want to give feedback, or think I should change my grades.


Enter fullscreen mode Exit fullscreen mode

8/10, does not allow precise units unless you want to use usleep, but still is limited to microseconds or seconds.


Using chrono and thread libraries, is as beautiful as:

Enter fullscreen mode Exit fullscreen mode

10/10, beautiful and expressive.


Using time package, it's pretty simple:

time.Sleep(3 * time.Second);
Enter fullscreen mode Exit fullscreen mode

10/10, direct and offers a simple way to convert between the units.


Enter fullscreen mode Exit fullscreen mode

8/10, simple but only accepts milliseconds as the parameter.


As far as my research and knowledge go, there's not a way of stoping the execution of Javascript. Still, there's something similar that can be achieved using a callback, like the following:

setTimeout(function() { doSomething(); }, 
Enter fullscreen mode Exit fullscreen mode

5/10, at least allow something similar to be done, but not in a good way.


Using time module:

Enter fullscreen mode Exit fullscreen mode

8/10, very practical, but requires the argument in seconds.


From my investigation, you can simply use a sleep function or use the ActiveSupport library:

Enter fullscreen mode Exit fullscreen mode

10/10, maybe the grade should be lower if using ActiveSupport is not something ordinary.


For being syntactically similar to C++, the sleep function is also similar to C++, using thread and time libraries:

Enter fullscreen mode Exit fullscreen mode

10/10, allow easy conversion between units.


As a close friend of Java, Scala's sleep is pretty much the same as Java:

Enter fullscreen mode Exit fullscreen mode

8/10, simple, direct, but requires milliseconds.


As I have absolutely no proficiency in Swift, I'll be sharing two options for this. The first one is based on Unix's sleep command, and the second one is closer to the implementation of a callback. (Based on this stack overflow question)

Option 1:

Enter fullscreen mode Exit fullscreen mode

Option 2, considering Swift >= 3:

let seconds = 3.0
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
    // Put your code which should be executed with a delay here
Enter fullscreen mode Exit fullscreen mode

6/10, does allow sleep in multiple ways but rely on Unix syntax or require you to use a Dispatch Queue and some other logic.



after 3000
Enter fullscreen mode Exit fullscreen mode

1/10, terrible name, but at least allows the sleep, and just for this will get one point.

Final thoughts

First of all, I must say that using a sleep in your code is not the best solution in most cases. Still, as I mentioned before, there are indeed scenarios where you do want that. Based on this, I was astonished at how easy it's in most languages to do what is desired.

The drawback of many languages is the necessity to remember precisely the type of unit you need to use on the call (gravitating between seconds and milliseconds).

Some languages seem to overcome such limitation through external libraries, which I think is a great solution and would be great if somehow this could be incorporated in newer releases of the language.

That all being said, I conclude this post with the confirmation: Tcl has the worst command for sleep!

Discussion (0)