Thread: timing method

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    15

    timing method

    Hey, I finally got something working with timing functions. Would this be a suitable technique for a simplistic text based game engine? Would there be any issues with it in the future? I can forsee an issue with stability if the engine using it couldn't run fast enough to pull under the CORE_TIME, but that could be increased.

    Commenting out the dummy loop unsync's the timing completely, with it in it lines up perfectly with my system clock. With no loop perhaps the difftime is just too small to register?

    Thoughts?

    edit: Just found an issue :P Changing CORE_TIME higher than 1 breaks it. tmp_timer controls the conditional display, and from what I have here, it won't fire off. This could also be the reason for it not working when removing the loop as tmp_timer doesn't increment to overcome x/core_time >= 1.

    edit2: Fixed the core_time problem, still hangs and messes up when removing the loop, probably can't fix that. cpu was running at 50% :P

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define CORE_TIME 1
    
    int main(void) {
    	double tot_time = 0.0;
    	time_t ref_time, chk_time;
    	int i = 0, a = 0, elapsed = 0;
    
    	while(1) {
    		ref_time = time(&ref_time);
    
    		for(i = 0; i < 10000; i++) {
    			a++;
    		}
    
    		chk_time = time(&chk_time);
    		tot_time += difftime(chk_time, ref_time);
    
    		if( (tot_time / CORE_TIME) >= 1 ) {
    			tot_time = 0;
    			elapsed += CORE_TIME;
    			printf("Time: %d Seconds.\n", elapsed);
    		}		
    	}
    	return 0;
    }
    Last edited by deepcode; 04-05-2011 at 02:43 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you don't want this to sit eating 100% CPU, you could look at something like select (that's a link, click it). It's not a standard C function, so you are looking at something compiler specific. But it's a way to have your program sort of sleep (which is another function that makes your program sit around waiting).

    The problem with using a loop that just increments a counter is that your compiler may just decide to optimize that out, and simply increment the couner by 10000 in one shot. There is no good standard to wait for a given time and then do something.

    But just looping makes your program eat up CPU cycles.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by deepcode View Post
    Hey, I finally got something working with timing functions. Would this be a suitable technique for a simplistic text based game engine? Would there be any issues with it in the future? I can forsee an issue with stability if the engine using it couldn't run fast enough to pull under the CORE_TIME, but that could be increased.

    Commenting out the dummy loop unsync's the timing completely, with it in it lines up perfectly with my system clock. With no loop perhaps the difftime is just too small to register?

    Thoughts?

    edit: Just found an issue :P Changing CORE_TIME higher than 1 breaks it. tmp_timer controls the conditional display, and from what I have here, it won't fire off. This could also be the reason for it not working when removing the loop as tmp_timer doesn't increment to overcome x/core_time >= 1.

    edit2: Fixed the core_time problem, still hangs and messes up when removing the loop, probably can't fix that. cpu was running at 50% :P

    Code:
    #include <stdio.h>
    #include <time.h>
    
    #define CORE_TIME 1
    
    int main(void) {
    	double tot_time = 0.0;
    	time_t ref_time, chk_time;
    	int i = 0, a = 0, elapsed = 0;
    
    	while(1) {
    		ref_time = time(&ref_time);
    
    		for(i = 0; i < 10000; i++) {
    			a++;
    		}
    
    		chk_time = time(&chk_time);
    		tot_time += difftime(chk_time, ref_time);
    
    		if( (tot_time / CORE_TIME) >= 1 ) {
    			tot_time = 0;
    			elapsed += CORE_TIME;
    			printf("Time: %d Seconds.\n", elapsed);
    		}		
    	}
    	return 0;
    }
    What OS is this on?

    Windows OS has timers you can use explicitly for this task. If you are only resolving to seconds, they will be more than accurate enough and they won't race your CPU into thermal meltdown.

    I'm sure Linux and Mac have similar appliances you can summon...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packet Container Class
    By ChaoticXSinZ in forum C++ Programming
    Replies: 2
    Last Post: 11-01-2010, 12:07 AM
  2. Best method for console timing system?
    By thegr8n8 in forum C++ Programming
    Replies: 2
    Last Post: 08-09-2010, 02:22 AM
  3. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  4. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-08-2008, 12:30 AM
  5. C# method
    By siten0308 in forum C# Programming
    Replies: 6
    Last Post: 07-15-2008, 07:01 AM