/** time.c - 跨平台 time 命令入口程序* 平台:Windows 使用 WinMain,Linux/macOS 使用 main*/#defineLIBTIME_IMPLEMENTATION#include"libtime.h"#ifdefined(_WIN32)||defined(_WIN64)/* Windows 入口:处理宽字符命令行参数 */#include<windows.h>/* 将宽字符命令行参数转换为多字节 */staticchar**convert_wargv(int argc,wchar_t* wargv[],int* out_argc){if(!wargv ||!out_argc)returnNULL;char**argv =(char**)malloc(sizeof(char*)* argc);if(!argv)returnNULL;for(int i =0; i < argc; i++){// 计算所需缓冲区大小int len =WideCharToMultiByte(CP_UTF8,0, wargv[i],-1,NULL,0,NULL,NULL);if(len <=0){libtime_free_args(argv, i);returnNULL;}// 分配并转换argv[i]=(char*)malloc(len);if(!argv[i]){libtime_free_args(argv, i);returnNULL;}WideCharToMultiByte(CP_UTF8,0, wargv[i],-1,argv[i], len,NULL,NULL);}*out_argc = argc;return argv;}/* Windows 标准入口函数 */int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd
){// 获取宽字符命令行参数int wargc =0;wchar_t**wargv =CommandLineToArgvW(GetCommandLineW(),&wargc);if(!wargv){fprintf(stderr,"Error: Failed to parse command line\n");return1;}// 转换为多字节参数int argc;char**argv =convert_wargv(wargc, wargv,&argc);LocalFree(wargv);// 释放宽字符参数if(!argv){fprintf(stderr,"Error: Failed to convert command line arguments\n");return1;}#else/* Linux/macOS 入口:使用标准 main 函数 */intmain(int argc,char* argv[]){// 直接使用标准C参数,无需转换#endif// 解析命令行参数libtime_format format;constchar* output_file;int append;constchar* custom_format;int cmd_start;if(libtime_parse_args(argc, argv,&format,&output_file,&append,&custom_format,&cmd_start)!=0){#ifdefined(_WIN32)||defined(_WIN64)libtime_free_args(argv, argc);#endifreturn1;}// 构建完整命令字符串(拼接命令和参数)size_t cmd_len =0;for(int i = cmd_start; i < argc; i++){cmd_len +=strlen(argv[i])+1;// 加空格}char* command =(char*)malloc(cmd_len +1);if(!command){fprintf(stderr,"Error: Out of memory\n");#ifdefined(_WIN32)||defined(_WIN64)libtime_free_args(argv, argc);#endifreturn1;}command[0]='\0';for(int i = cmd_start; i < argc; i++){if(i > cmd_start)strcat(command," ");strcat(command, argv[i]);}// 执行命令并测量时间libtime_result result;int ret =libtime_execute(command,&result);free(command);if(ret !=0){fprintf(stderr,"Error: Failed to execute command (error code: %d)\n", ret);#ifdefined(_WIN32)||defined(_WIN64)libtime_free_args(argv, argc);#endifreturn1;}// 准备输出流FILE* output =stderr;if(output_file){output =fopen(output_file, append ?"a":"w");if(!output){fprintf(stderr,"Error: Failed to open output file '%s'\n", output_file);#ifdefined(_WIN32)||defined(_WIN64)libtime_free_args(argv, argc);#endifreturn1;}}// 输出测量结果if(custom_format){for(size_t i =0; i <strlen(custom_format); i++){if(custom_format[i]=='%'&& i +1<strlen(custom_format)){i++;switch(custom_format[i]){case'e':fprintf(output,"%.3f", result.real_time);break;case'U':fprintf(output,"%.3f", result.user_time);break;case'S':fprintf(output,"%.3f", result.sys_time);break;case'P':fprintf(output,"%.0f", result.real_time >0?(result.user_time + result.sys_time)/ result.real_time *100:0);break;case'M':fprintf(output,"%ld", result.max_rss);break;case'x':fprintf(output,"%d", result.exit_status);break;default:fputc(custom_format[i], output);break;}}else{fputc(custom_format[i], output);}}fprintf(output,"\n");}else{libtime_print(&result, format, output);}// 清理资源if(output_file)fclose(output);#ifdefined(_WIN32)||defined(_WIN64)libtime_free_args(argv, argc);#endifreturn result.exit_status;}
编译
gcc time_cli.c -lpsapi
测试输出
timecli.exe -f"=== 执行统计 ===\n实际时间: %e秒\n用户CPU: %U秒\n系统CPU: %S秒\n内存峰值: %M KB\nCPU使用率: %P\n退出代码: %x"dirtime.exe python selp.py
def print_self_source():# __file__ 变量包含当前脚本的路径with open(__file__, 'r') as f:# 读取并打印文件内容print(f.read())if __name__ =="__main__":print_self_source()0.104 real 0.016 user 0.000 systime.exe -pping baidu.comPinging baidu.com [182.61.244.181] with 32 bytes of data:
Reply from 182.61.244.181: bytes=32time=102ms TTL=49
Reply from 182.61.244.181: bytes=32time=125ms TTL=49182.61.244.181 的 Ping 统计信息:
^ 数据包: 已发送 =2,已接收 =2,丢失 =0(0% 丢失),
往返行程的估计时间(以毫秒time.exe -v gcc --version
gcc (x86_64-posix-seh-rev0, Built by MinGW-Builds project)15.1.0
Copyright (C)2025 Free Software Foundation, Inc.
This is free software; see the sourcefor copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.Command exited with status 0
Real time: 0.068 seconds
User CPU time: 0.016 seconds
Sys CPU time: 0.016 seconds
Max RSS: 5400 KB
作为库使用
#defineLIBTIME_IMPLEMENTATION#include"libtime.h"#include<stdio.h>intmain(){libtime_result result;constchar* command;// 根据平台选择测试命令#ifdef_WIN32command ="dir";// Windows 命令#elsecommand ="ls -l";// Linux/macOS 命令#endifprintf("执行命令: %s\n", command);int ret =libtime_execute(command,&result);if(ret !=0){fprintf(stderr,"命令执行失败,错误码: %d\n", ret);return1;}// 打印详细时间统计printf("\n命令执行统计:\n");libtime_print(&result, LIBTIME_FORMAT_VERBOSE,stdout);return0;}