shell脚本 统计一段程序运行时间,如何精确到毫秒级别

JoinTencent 2016-04-29 10:45:59
各位大神:
遇到一个问题,要用shell脚本 统计一段程序运行时间,精确到毫秒级别,在网上找到一种方法是用date命令,程序如下:

#!/bin/bash
#var= #声明全局变量
function getTiming()
{
exec_start=$1
exec_end=$2

exec_start_s=`echo $exec_start | cut -d "." -f 1` #获取开始时间的秒
exec_start_ns=`echo $exec_start | cut -d "." -f 2` #获取开始时间的纳秒
exec_end_s=`echo $exec_end | cut -d "." -f 1` #获取结束时间的秒
exec_end_ns=`echo $exec_end | cut -d "." -f 2` #获取结束时间的纳秒

echo $exec_start_s
echo ""
echo $exec_start_ns
echo ""
echo $exec_end_s
echo $exec_end_ns

exec_time_ms=$[$[$[ 10#$exec_end_s - 10#$exec_start_s ] * 1000] + $[$[10#$exec_end_ns / 1000000] - $[10#$exec_start_ns / 1000000] ] ]
nowdate=`date +%Y%m%d-%T`
echo "--------$nowdate-------->":$exec_time_ms
}

start=$(date +%s.%N)
ls >& /dev/null    # hey, be quite, do not output to console....
end=$(date +%s.%N)
echo $start
echo $end
getTiming $start$end

但是运行结果却是这样的:
1461897214.107974859
1461897214.124368167
1461897214

1079748591461897214



--------20160429-10:33:34-------->:-2541645805461

这是为什么呀?
...全文
1836 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-04-29
  • 打赏
  • 举报
回复
仅供参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _MSC_VER
    #pragma warning(disable:4996)
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef _MSC_VER
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef _MSC_VER
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef _MSC_VER
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
//1-79行添加到你带main的.c或.cpp的那个文件的最前面
//81-86行添加到你的main函数开头
//90-94行添加到你的main函数结束前
//在要写LOG的地方仿照第88行的写法写LOG到文件MyLog1.log中
alinly 2016-04-29
  • 打赏
  • 举报
回复
shell中有变量,自我标记: echo $SECONDS

23,207

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧