有時在執行完一段windows的批處理後,想知道這個過程花費了多少時間,若是是windows下的c代碼能夠在過程先後分別調用GetTickCount(),而後相減便可獲得花費的時間。linux
可是若是在批處理中就沒有這樣現成的函數,而且在本人在網上找了很久都沒找到。最後在搞定了批處理變量計算,從exe中取得返回值等技術點後,最終實現了這個功能。shell
下面的代碼將打印出20windows
1 @echo off 2 set cho=23 3 set /a res=%cho% - 3 4 echo %res%
注意,第一個set後面=先後必定不能加空格,第二個set後必定得有/abash
用%errorlevel%能夠取得執行一個exe以後其返回值。函數
@echo off start /wait Program.exe set r=%errorlevel% echo %r%
寫一個簡單的c程序,調用GetTickCount()將其值返回spa
#include <windows.h> #include <stdio.h> int main(int argc, char** argv) { int t = GetTickCount(); printf("%d\n", t); return t; }
隨便用一個c的編譯器,將上面的c源碼編譯成exe程序,取名爲GetTickCount.exe,並將其放到某個系統路徑下。code
@echo off start /wait GetTickCount.exe set t1=%errorlevel% sleep 3
::TODO Something
start /wait GetTickCount.exe set t2=%errorlevel% set /a t=%t2%-%t1% echo %t%
最後將打印出以毫秒爲單位的時間花費。blog
GetTickCount的下載路徑 http://files.cnblogs.com/files/xiangism/GetTickCount.rarget
順便貼出如何在linux下的shell中實現計時編譯器
#!/bin/bash start=$(date "+%s") #do something sleep 2 now=$(date "+%s") time=$((now-start)) echo "time used:$time seconds"
~~~~Eureka~~~~