GNU/Linux >> Belajar Linux >  >> Linux

Dapatkan waktu eksekusi program di shell

Gunakan time bawaan kata kunci:

$ help time

time: time [-p] PIPELINE
    Execute PIPELINE and print a summary of the real time, user CPU time,
    and system CPU time spent executing PIPELINE when it terminates.
    The return status is the return status of PIPELINE.  The `-p' option
    prints the timing summary in a slightly different format.  This uses
    the value of the TIMEFORMAT variable as the output format.

Contoh:

$ time sleep 2
real    0m2.009s
user    0m0.000s
sys     0m0.004s

Anda bisa mendapatkan informasi yang jauh lebih detail daripada time bawaan bash (yang disebutkan oleh Robert Gamble) menggunakan waktu(1). Biasanya ini adalah /usr/bin/time .

Contoh keluaran verbose:

$ /usr/bin/time -v sleep 1
       Command being timed: "sleep 1"
       User time (seconds): 0.00
       System time (seconds): 0.00
       Percent of CPU this job got: 1%
       Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.05
       Average shared text size (kbytes): 0
       Average unshared data size (kbytes): 0
       Average stack size (kbytes): 0
       Average total size (kbytes): 0
       Maximum resident set size (kbytes): 0
       Average resident set size (kbytes): 0
       Major (requiring I/O) page faults: 0
       Minor (reclaiming a frame) page faults: 210
       Voluntary context switches: 2
       Involuntary context switches: 1
       Swaps: 0
       File system inputs: 0
       File system outputs: 0
       Socket messages sent: 0
       Socket messages received: 0
       Signals delivered: 0
       Page size (bytes): 4096
       Exit status: 0

#!/bin/bash
START=$(date +%s)
# do something
# start your script work here
ls -R /etc > /tmp/x
rm -f /tmp/x
# your logic ends here
END=$(date +%s)
DIFF=$(( $END - $START ))
echo "It took $DIFF seconds"

Linux
  1. Bagaimana Cara Mendapatkan Alamat Ip Eksternal Dalam Script Shell?

  2. Bagaimana Mendapatkan Deskripsi Opsi `shopt` yang Tersedia?

  3. 7 Contoh Strace untuk Debug Eksekusi Program di Linux

  1. Cara portabel untuk mendapatkan ukuran file (dalam byte) di shell?

  2. Cara mendapatkan ukuran file tar.gz dalam (MB) dengan python

  3. Bagaimana cara mendapatkan PYTHONPATH di shell?

  1. Bagaimana Anda membuat Ubuntu menjalankan program secara otomatis setiap kali layar tidak terkunci?

  2. Bagaimana cara mendapatkan ukuran direktori sebenarnya (keluar dari du)?

  3. Atur waktu waktu eksekusi beberapa perintah