You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何统计Bash别名的执行耗时?(附临时解决方案)

嵌入式Linux系统中Bash别名的计时优化问题

我的Dreadnaught嵌入式Linux系统里有不少Bash别名,部分别名包含大量任务。现在要优化这些任务的执行效率,需要统计不同任务排列的耗时,找出最快的方案。

拆分构建脚本时各部分都能正常运行,但组合后就失效。排查发现:命令替换在定义别名时就会被求值,而不是执行别名的时候。

尝试过以下方法但均未成功:

  • 创建可自计时的别名(用于统计自身及其他进程耗时)
  • 使用函数实现计时
  • 使用time命令,但该命令无法接收别名作为参数,且别名嵌套调用其他别名和系统命令,难以展开

临时解决方案

目前写了一个Bash脚本作为临时方案,直接调用计时函数统计任务耗时,代码如下:

#!/bin/bash

echo "== Task 1 Start =="
timerstart1=$(eval 'date +%s')
TimerStartstr1=$(eval 'date -d@$((timerstart1))')
echo "Task 1 started at   $timerstart1 = $TimerStartstr1"

myTask1

timerend1=$(eval 'date +%s')
TimerEndstr1=$(eval 'date -d@$timerend1')
echo "Task 1 completed at $timerend1 = $TimerEndstr1"
duration1=$(($timerend1 - $timerstart1))
echo "=========== Task 1 Complete, took $duration1 =================="


echo "== Task 2 Start =="
timerstart2=$(eval 'date +%s')
TimerStartstr2=$(eval 'date -d@$((timerstart2))')
echo "Task 2 started at   $timerstart2 = $TimerStartstr2"

MyTask2

timerend2=$(eval 'date +%s')
TimerEndstr2=$(eval 'date -d@$timerend2')
echo "Task 2 completed at $timerend2 = $TimerEndstr2"
duration2=$(($timerend2 - $timerstart2))
echo "=========== Task 2 Complete, took $duration2 =================="

echo "Speed improvement = $(($duration1 - $duration2))"

测试结果

测试发现所谓的“优化版”Task2耗时反而更长(83秒vs原版本70秒)。

之前失败的零散尝试

之前尝试的零散代码(单独运行基本正常,组合后失效):

[user@Dreadnaught log]$ echo ==Start==; timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr;
==Start==
Started at   1710780061 = Mon Mar 18 16:41:01 GMT 2024
[user@Dreadnaught log]$ timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$timerend'); echo Completed at $timerend = $TimerEndstr;
Completed at 1710780075 = Mon Mar 18 16:41:15 GMT 2024
[user@Dreadnaught log]$ echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) ==================
=========== TestTimerTask Complete, took 14 ==================
[user@Dreadnaught log]$ alias timertest="echo ==Start==; timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr; read -p 'Waiting for input' >/dev/tty; timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$timerend'); echo Completed at $timerend = $TimerEndstr; echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) =================="
[user@Dreadnaught log]$ timertest
==Start==
-ash: Mar: not found
Started at   1710780061 = Mon Mar 18 16:41:01 GMT 2024
Waiting for input
-ash: Mar: not found
Completed at 1710780075 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 14 ==================
[user@Dreadnaught log]$ timertest
==Start==
-ash: Mar: not found
Started at   1710780061 = Mon Mar 18 16:41:01 GMT 2024
Waiting for input
-ash: Mar: not found
Completed at 1710780075 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 14 ==================
[user@Dreadnaught log]$ alias timertest="echo ==Start==; timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr; sleep 2; timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$timerend'); echo Completed at $timerend = $TimerEndstr; echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) =================="
[user@Dreadnaught log]$ timertest
==Start==
-ash: Mar: not found
Started at   1710780100 = Mon Mar 18 16:41:01 GMT 2024
-ash: Mar: not found
Completed at 1710780101 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 1 ==================
[user@Dreadnaught log]$ 


[user@Dreadnaught log]$ alias timerstart="f(){ timerstart=$(eval 'date +%s'); TimerStartstr=$(eval 'date -d@$((timerstart))'); echo Started at \ \ $timerstart = $TimerStartstr; unset -f f; };f"
[user@Dreadnaught log]$ alias timerend="f(){ timerend=$(eval 'date +%s'); TimerEndstr=$(eval 'date -d@$((timerend))'); echo Finished at \ \ $timerend = $TimerEndstr; unset -f f; };f; echo =========== TestTimerTask Complete, took $(($timerend - $timerstart)) =================="
[user@Dreadnaught log]$ alias timertest="timerstart; read -p 'Waiting for input' >/dev/tty; timerend"
[user@Dreadnaught log]$ timertest
-ash: Mar: not found
Started at   1710780677 = Mon Mar 18 16:41:01 GMT 2024
Waiting for input
-ash: Mar: not found
Finished at   1710780677 = Mon Mar 18 16:41:15 GMT 2024
=========== TestTimerTask Complete, took 0 ==================
[user@Dreadnaught log]$ 

内容的提问来源于stack exchange,提问作者Scott

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:15:58