~berfr/berfr.me

bf0e0803fcdb3573c896e39b6f34eeac0a1e3882 — berfr 4 months ago bd9bdb6 20-04-30-22-28
Rewrap text in execution-time post
1 files changed, 24 insertions(+), 6 deletions(-)

M content/posts/execution-time.md
M content/posts/execution-time.md => content/posts/execution-time.md +24 -6
@@ 27,7 27,9 @@ user    0m12.868s
sys     0m0.008s
```

This result is expected; it takes a certain amount of time to perform more than one hundred million additions. During this, my computer was working hard; the fans started going pretty fast.
This result is expected; it takes a certain amount of time to perform more than
one hundred million additions. During this, my computer was working hard; the
fans started going pretty fast.

Now consider this Python program:



@@ 52,9 54,17 @@ user    0m0.026s
sys     0m0.008s
```

This time though, my computer was completely silent during the execution. As if nothing was being done. If you look at the source of [`time.sleep`], and to the called cross platform [`pysleep`] function, you will see that the underlying mechanism for wasting time is a call to [`select`] with no watched file descriptor sets and the desired timeout. From this point, it is up to the operating system to return when the amount of time is elapsed.
This time though, my computer was completely silent during the execution. As if
nothing was being done. If you look at the source of [`time.sleep`], and to the
called cross platform [`pysleep`] function, you will see that the underlying
mechanism for wasting time is a call to [`select`] with no watched file
descriptor sets and the desired timeout. From this point, it is up to the
operating system to return when the amount of time is elapsed.

The actual CPU time can be seen in the values returned by the [`time`] command. The `user` and `sys` parts is the time that was taken for running code in user-mode and in kernel-mode respectively. The first program spent all the time running code whereas the second spent almost no time running code.
The actual CPU time can be seen in the values returned by the [`time`] command.
The `user` and `sys` parts is the time that was taken for running code in
user-mode and in kernel-mode respectively. The first program spent all the time
running code whereas the second spent almost no time running code.

[`time.sleep`]: https://github.com/python/cpython/blob/62183b8d6d49e59c6a98bbdaa65b7ea1415abb7f/Modules/timemodule.c#L326
[`pysleep`]: https://github.com/python/cpython/blob/62183b8d6d49e59c6a98bbdaa65b7ea1415abb7f/Modules/timemodule.c#L1859


@@ 91,7 101,10 @@ user    0m0.294s
sys     0m0.002s
```

The computation time is a fraction of a second. At first, I was certain a compiler optimization had avoided the computation and just printed the obvious result. By inspecting the Assembly for this program, we can see that the loop and arithmetic is actually performed:
The computation time is a fraction of a second. At first, I was certain a
compiler optimization had avoided the computation and just printed the obvious
result. By inspecting the Assembly for this program, we can see that the loop
and arithmetic is actually performed:

```asm
...


@@ 102,6 115,11 @@ The computation time is a fraction of a second. At first, I was certain a compil
...
```

I was aware of overhead in interpreted languages such as Python but never thought it would be this obvious.
I was aware of overhead in interpreted languages such as Python but never
thought it would be this obvious.

This simple experiment shows how execution time varies in very similar programs. I would much prefer waiting only 0.2 seconds rather than 12 for my computations to complete. At the same time, I would choose writing in a higher level programming language any day. As with many other things in life, it is a matter of tradeoffs.
This simple experiment shows how execution time varies in very similar programs.
I would much prefer waiting only 0.2 seconds rather than 12 for my computations
to complete. At the same time, I would choose writing in a higher level
programming language any day. As with many other things in life, it is a matter
of tradeoffs.