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.
C
1
sleep(3);
8/10, does not allow precise units unless you want to use usleep
, but still is limited to microseconds or seconds.
C++11
Using chrono and thread libraries, is as beautiful as:
1
std::this_thread::sleep_for(std::chrono::seconds(3));
10/10, beautiful and expressive.
Go
Using time package, it’s pretty simple:
1
time.Sleep(3 * time.Second);
10/10, direct and offers a simple way to convert between the units.
Java
1
Thread.sleep(3000);
8/10, simple but only accepts milliseconds as the parameter.
JavaScript
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:
1
2
setTimeout(function() { doSomething(); },
3000);
5/10, at least allow something similar to be done, but not in a good way.
Python
Using time module:
1
time.sleep(3)
8/10, very practical, but requires the argument in seconds.
Ruby
From my investigation, you can simply use a sleep function or use the ActiveSupport library:
1
sleep(2.minutes)
10/10, maybe the grade should be lower if using ActiveSupport is not something ordinary.
Rust
For being syntactically similar to C++, the sleep function is also similar to C++, using thread and time libraries:
1
thread::sleep(time::Duration::from_secs(3));
10/10, allow easy conversion between units.
Scala
As a close friend of Java, Scala’s sleep is pretty much the same as Java:
1
Thread.sleep(3000)
8/10, simple, direct, but requires milliseconds.
Swift
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:
1
sleep(3)
Option 2, considering Swift >= 3:
1
2
3
4
let seconds = 3.0
DispatchQueue.main.asyncAfter(deadline: .now() + seconds) {
// Put your code which should be executed with a delay here
}
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.
Tcl
Argh!
1
after 3000
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!