simple command line timer for windows @ 7/27/2011

programming & research
I have just switched to working on Windows. And I found that there is no such thing as the time command line tool in Linux. So I decided to write one myself. I don't need many features. Counting the number of seconds a command takes is enough. I used the QueryPerformanceCounter() function to get the accurate time. So it is not portable to Linux or other platforms.

Anyone who is interested can modify the code at its own will. But I would appreciate it if you can give me your improved version of the code. I have also attached a compiled version. It works on Windows Server 2008 R2 and Windows 7. Other Windows versions are not tested.

Use it like this:
    C:\> timer command [arguments]

It gives:
    [command outputs]...
    --------------
    time elapsed: 0.123s

Download:
http://www.mediafire.com/?vzf4yv5xs9saemu
http://www.rayfile.com/zh-cn/files/f5a8ae19-b81c-11e0-93f2-0015c55db73d/


-----------------------------------
Source code:

#define NOMINMAX
#include <windows.h>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;

double gettime(){
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nTime);
    return (double)nTime.QuadPart*1000/nFreq.QuadPart;
}

int main(int argc, char ** argv){
    if(argc<2 || argc>=2 && string(argv[1])=="--help"){
        cout<<endl;
        cout<<"This is a very very simple timing tool by hjk41."<<endl;
        cout<<endl;
        cout<<"Use it this way: "<<endl;
        cout<<"\tC:\\>"<<argv[0]<<" command [arguments]"<<endl;
        cout<<endl;
        return 0;
    }

    double start=gettime();

    string cmd=argv[1];
    for(int i=2;i<argc; i++){
        cmd= cmd+" "+argv[i];
    }
    system(cmd.c_str());

    double end=gettime();
    double exe_time=(end-start)/1000;

    cout<<"----------------"<<endl;
    cout<<"time elapsed: "<<exe_time<<"s"<<endl;

    return 0;
}




--------------------------------------
Chinese Edition, ignore this if you cannot read Chinese. :-)

Windows下的简单计时程序。
使用方法
    C:\> timer 命令 [参数]

结果:
    [命令输出]...
    --------------
    time elapsed: 0.123s

下载链接:
http://www.rayfile.com/zh-cn/files/f5a8ae19-b81c-11e0-93f2-0015c55db73d/
http://www.mediafire.com/?vzf4yv5xs9saemu

上面有源码。你可以随便改,但是如果有什么改进,希望你能告诉我,我好更新一下。 :-)
发布于 7/27/2011 15:05:22 | 评论:0

看帖要回帖...