How to implement "time" in Windows ?

This is a discussion on How to implement "time" in Windows ? within the Windows Programming forums, part of the Platform Specific Boards category; In *nix, there the time utility for measuring the execution time of a command I'm interested in writing my own, ...

  1. #1
    TmX
    TmX is offline
    Registered User TmX's Avatar
    Join Date
    Sep 2006
    Posts
    12

    How to implement "time" in Windows ?

    In *nix, there the time utility for measuring the execution time of a command

    I'm interested in writing my own, so here's my quick attempt :
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[]){
    	clock_t start,end;
    	int x;
    	char command[100];
    
    	if (argc > 1){
    		strcpy(command," ");
    
    		for (x = 1; x < argc; x++){
    			strcat(command,argv[x]);
    			strcat(command," ");
    		}
    
    		start = clock();
    		system(command);
    		end = clock();
    		printf("%f\n",(float)(end-start)/CLOCKS_PER_SEC);
    	}
    	return 0;
    
    }
    I don't think it perform correctly, probably due to the overhead caused by system().
    How can I make it more accurate ? Maybe some Win32 API needed ?

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Describe "not working properly". Yes, system will have some overhead, but that's not necessarily the only reason. If you really want to precisely measure another process, you probably will want to use CreateProcess() so that you can control the process better, and use GetProcessTimes() to get the time used by that process.

    Yes, that's a whole lot more complicated, but it's more precise - unfortunately, there's no SIMPLE way to do that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Top Level Windows
    By quark_77 in forum Windows Programming
    Replies: 0
    Last Post: 07-13-2008, 03:32 PM
  2. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 06:48 PM
  3. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 03:55 PM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 06:31 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21