/** 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;}
關注gongzhonghao【CVPR頂會精選】1.導讀1.1 論文基本信息論文標題:《HOLODECK: Language Guided Generation of 3D Embodied AI Environments》作者:Yue Yang*1, Fan-Yun Sun*2, Luca Weihs*4, Eli Vanderbilt4, Alvaro Herrasti4,Winson Han4, Jiajun …
HTML 框架:構建網頁布局的基石
引言
HTML 框架是網頁設計中不可或缺的一部分,它為網頁內容的布局提供了強大的支持。本文將深入探討 HTML 框架的概念、種類、應用以及如何有效地使用它們來構建網頁布局。
什么是 HTML 框架?
HTML 框架是一種網…