Want to analyze how long the wall clock, kernel time, etc., a Linux program takes time to run? Whether for performance testing, code optimization or just general curiosity, this quick guide will get you started.
Linux program programming
Timing a Linux program helps to understand how much time was spent. The versatile Linux time
The command can be used for this. the time
The command measures real time (In other words, the time of the wall clock), user and system. User time is the time that the program runs in user mode, or put another way, outside the kernel. System time is the time the program runs inside the kernel.
It is essential to pay attention that both user time and system time are the actual CPU time spent within user mode and within the kernel., respectively. In other words, when a program is locked for a while and is not using the CPU, that time will not count for Username O sys
times. Knowing this, we can accurately measure how much effective CPU time was used (combining them).
El Linux weather Tool
Given the Username and sys
times report only CPU time, while true time reports the real time of the wall clock, it is (because) very common to see the time
tool return output where a combination of Username + sys it is not the same true weather. An example can be seen when timing sleep
:
time sleep 1
Here we time the sleep
command using the time
tool. As we can see, our true weather (1.001 seconds) matches the time on our wall clock and the requested time (sleep 1
ask for a dream of a second) very well. We also see that very little CPU time had to be dedicated to the command as a whole: to combine Username + sys weather, we see that they only spent 0.001 seconds.
We can also, probably incorrectly, deduce that the kernel was not involved in this command, since the sys time is indeed 0. Despite this, As the time
indicates the manual: “Cuando el tiempo de ejecución de un comando es muy cercano a cero, some values (as an example, the percentage of CPU used) can be reported as zero (which is wrong) o como un signo de interrogación”.
Using weather To measure performance
We can use time
to examine how long the given actions will take (In other words, wall clock time) and how much CPU time they consumed while doing it. As a simple example, we could examine if any filesystem cache is working on our system. To do it, we could jump to /usr
directory, which could easily contain 200k to 500k files in a common Linux installation.
Once there, we can use the find
tool, timed by time
para examinar cuánto tiempo tomaría escanear todas las carpetas y listar todos los archivos en el /usr
directory:
cd /usr
time find . >/dev/null 2>&1
As we can see, se necesitan 12.484 segundos para listar todos los archivos en el /usr
directory (y debajo de él). Redirigimos la salida stdout (standard output) del comando a >/dev/null
y además redirigir cualquier error stderr (standard error) a /dev/null
a través de el uso de una redirección de stderr a stdout, In other words 2>&1
.
Además vemos que nuestro tiempo de CPU es 1.043 seconds (Username) + 2.908 seconds (sys) for a total of 3.951 segundos de tiempo de CPU.
Probémoslo en otra ocasión borrando nuestra (s) caché (s) de inodo (y otras):
sync; echo 3 | sudo tee /proc/sys/vm/drop_caches cd /usr time find . >/dev/null 2>&1
El primer comando eliminará el caché de inodos, dentries (entradas de directorio) y pagecache. This time, el resultado volvió un poco más rápido, with 1,255 segundos guardados en el comando. Probablemente una caché basada en disco físico ayudó aquí.
Para demostrar qué tan bien funciona el almacenamiento en caché de Linux en general, let's rerun the command, but this time without removing Linux caches:
What a difference! We see a huge decrease in the time required in all three timed areas and our command runs in less than half a second!!
Using weather For code optimization
Once we are comfortable using the time
command on the command line, we can expand its use to get the most out of our scripts and bash code. As an example, a commonly used approach among some professionals is to execute a certain command usually, What 1000 executions, and calculate the total time (or average) of these executions.
Then, alternative command can be used. That alternate command (or solution / implementation, In other words, multiple commands taken together as a single piece of code to be timed) can be timed again. En Linux (or more specifically in Bash encoding, etc.), there are often many alternatives to address a given obstacle; In general, there are multiple tools available to obtain / achieve the same result.
Testing which one works best optimizes program execution time and potentially other factors such as E / S from disk (reducing disc wear) or memory usage (allowing more programs to run in the same instance). To make the most of the wall clock time, a certain tool uses, on average, así como el tiempo de CPU consumido por la herramienta (otro factor / consideración importante de optimización) se puede medir a través de el time
tool.
Exploremos un ejemplo práctico del uso de la línea de comandos para ejecutar un comando que queremos utilizar en uno de nuestros scripts. El comando obtendrá una lista de procesos y mostrará la segunda columna. Usamos ambos awk
and sed
to do it, y ejecute cada comando 1000 veces para ver la diferencia en el rendimiento general.
time for ((i=1;i<=1000;i++)); do ps -ef | awk '{print $2}' >/dev/null 2>&1; done time for ((i=1;i<=1000;i++)); do ps -ef | but 's|^[^ ]+[ t]+||;s|[ t].*||' >/dev/null 2>&1; done
Even when it seems more complex (use a double regular expression to parse the second column), our second command is a bit faster than our first command when it comes to wall clock time.
Using a very similar configuration (In other words time for ((i=1;i<=1000;i++)); do command_to_be_timed >/dev/null 2>&1; done
where command_to_be_timed is the command to be tested for wall clock or CPU time), one can test the time of any command or set of commands (as is the case here; we use so much the ps
and awk
/sed
commands).
Follow these steps for various time-consuming commands (in any linux script) will help us to reduce the overall execution time and / O (if you optimize to reduce CPU time) the system load of our scripts.
If you want to learn more about regular expressions, you are probably interested in How to Modify Text Using Regular Expressions with sed Stream Editor.
Ending
In this post, we explore Linux time
command. We clarify that true, Username and sys times indicate and how the last two relate to CPU usage. We also review several examples of how to use weather in a practical way.
If you liked reading this post, take a look at Declarations, errors and crashes: What is the difference? and What is Stack Smashing? It can be fixed ?.