Linux下计算command执行时间的方法

Linux下计算command执行时间的方法:
方法一:time cmd

#!/bin/bash
time fio --name=test --filename=/dev/nvme0n1 --ioengine=libaio --direct=1 --rw=write --bs=1024k --iodepth=128 --numjobs=1 --size=20G

直接在命令之前加上time 命令,cmd执行结束后,会连带执行结果与执行时间一起输出,不会影响原cmd执行结果,如此例的结果如下,其中real为cmd执行消耗的时间:

test: (g=0): rw=write, bs=(R) 1024KiB-1024KiB, (W) 1024KiB-1024KiB, (T) 1024KiB-1024KiB, ioengine=libaio, iodepth=128
fio-3.1
Starting 1 process
Jobs: 1 (f=1): [W(1)][100.0%][r=0KiB/s,w=3300MiB/s][r=0,w=3300 IOPS][eta 00m:00s]
test: (groupid=0, jobs=1): err= 0: pid=5038: Mon Jun  5 14:01:14 2023
  write: IOPS=3326, BW=3327MiB/s (3488MB/s)(20.0GiB/6156msec)
    slat (usec): min=17, max=258, avg=77.40, stdev=30.09
    clat (usec): min=521, max=85079, avg=38374.01, stdev=12812.81
     lat (usec): min=655, max=85199, avg=38451.58, stdev=12811.39
    clat percentiles (usec):
     |  1.00th=[  586],  5.00th=[ 9634], 10.00th=[36439], 20.00th=[37487],
     | 30.00th=[38011], 40.00th=[38011], 50.00th=[38011], 60.00th=[39060],
     | 70.00th=[39060], 80.00th=[39060], 90.00th=[39060], 95.00th=[70779],
     | 99.00th=[77071], 99.50th=[78119], 99.90th=[83362], 99.95th=[84411],
     | 99.99th=[85459]
   bw (  MiB/s): min= 3216, max= 3430, per=100.00%, avg=3328.50, stdev=67.11, samples=12
   iops        : min= 3216, max= 3430, avg=3328.50, stdev=67.11, samples=12
  lat (usec)   : 750=3.38%, 1000=0.04%
  lat (msec)   : 2=0.16%, 4=0.32%, 10=1.17%, 20=1.76%, 50=85.19%
  lat (msec)   : 100=7.98%
  cpu          : usr=19.16%, sys=10.90%, ctx=17052, majf=0, minf=11
  IO depths    : 1=0.1%, 2=0.1%, 4=0.1%, 8=0.1%, 16=0.1%, 32=0.2%, >=64=99.7%
     submit    : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.0%
     complete  : 0=0.0%, 4=100.0%, 8=0.0%, 16=0.0%, 32=0.0%, 64=0.0%, >=64=0.1%
     issued rwt: total=0,20480,0, short=0,0,0, dropped=0,0,0
     latency   : target=0, window=0, percentile=100.00%, depth=128

Run status group 0 (all jobs):
  WRITE: bw=3327MiB/s (3488MB/s), 3327MiB/s-3327MiB/s (3488MB/s-3488MB/s), io=20.0GiB (21.5GB), run=6156-6156msec

Disk stats (read/write):
  nvme0n1: ios=163/40599, merge=0/0, ticks=8/1452412, in_queue=1453156, util=97.66%

real    0m6.619s
user    0m1.326s
sys     0m0.849s

方法二:date +%s

start_time=`date +%s`
fio --name=test --filename=/dev/nvme0n1 --ioengine=libaio --direct=1 --rw=write --bs=1024k --iodepth=128 --numjobs=1 --size=20G
end_time=`date +%s`
echo "$((end_time-start_time))"

以此为例,date +%s 可以将时间转换为s,这样在cmd前后加上此命令抓取时间,那么前后差值就是cmd执行的时间了,这个用起来比较灵活,可以统计脚本一段
过程的总耗时
额外提一下,date +%s的效果即作用:

root@unassigned:~/Desktop/xian# date +%s
1685945376

这个就是输出的结果
这个命令可以用于获取当前时间的 Unix 时间戳(即距离1970年1月1日00:00:00 UTC的秒数)。
计算方式如下:
在 Unix 系统中,时间戳以 UTC(协调世界时)的形式表示。该时间是相对于时间 1970 年 1 月 1 日,00:00:00 UTC 的秒数。
因此,通过计算当前时间和1970年1月1日00:00:00 UTC的时间差,可以得到当前时间的 Unix 时间戳。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux中,可以使用多种方法计算程序的执行时间。以下是一些常用的方法: 1. time命令:time命令可以用于测量程序的执行时间。在终端中运行以下命令: ``` time <command> ``` 其中,`<command>`是要执行的程序或命令。time命令会输出程序的实际执行时间、用户态时间和系统态时间。 2. clock函数:在C/C++编程中,可以使用clock函数来计算程序的执行时间。需要包含头文件`<time.h>`,并在程序中使用以下代码: ```c #include <stdio.h> #include <time.h> int main() { clock_t start, end; double cpu_time_used; start = clock(); // 执行需要计时的代码 end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf("程序执行时间:%f 秒\n", cpu_time_used); return 0; } ``` 这段代码会输出程序的执行时间,单位为秒。 3. gettimeofday函数:在C/C++编程中,可以使用gettimeofday函数来获取当前时间,并通过计算时间差来得到程序的执行时间。需要包含头文件`<sys/time.h>`,并在程序中使用以下代码: ```c #include <stdio.h> #include <sys/time.h> int main() { struct timeval start, end; long seconds, microseconds; gettimeofday(&start, NULL); // 执行需要计时的代码 gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; microseconds = end.tv_usec - start.tv_usec; double elapsed = seconds + microseconds / 1000000.0; printf("程序执行时间:%f 秒\n", elapsed); return 0; } ``` 这段代码会输出程序的执行时间,单位为秒。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值