Thread: How To Stop A Program After 10 Seconds?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    33

    How To Stop A Program After 10 Seconds?

    I am working on a program to make topological sorts, but depending on size of the dataset given the time to compute everything is too large.

    I want to add a command to my program to make it track its execution time and abort automatically should that time go over 10 seconds.

    Does anyone know how to do this?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    I think the time.h library will be a good place to start. here is a basic example:

    FAQ: How do i display current time and date ?

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    If you have something like a main loop, you can add a check to observe a time difference between the present and the starting time.
    If it exceeds your limit, just break;

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sure, but let me put in a plug for the forum members here. We have some OUTSTANDING sorters on this forum. Why not let them guide you to a better sort algorithm or implementation, than you are currently using - and THEN set up your program to stop sorting after 10 seconds?

    You might well be amazed at how much more can be done with an optimized sort algorithm and implementation!

    Including time.h and comparing the start and current output from time(&NULL), is probably best. When the current time is more than 10 seconds above the starting time, then have your program break out of the loop with an expired flag variable set and return, perhaps.

    Ninja-posted again!

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Topological sorting - Wikipedia, the free encyclopedia is radically different from your classic sorting problem. It's not one that I can recall ever having a need for and not something that I've coded up already.

    Wikipedia states that "The usual algorithms for topological sorting have running time linear in the number of nodes plus the number of edges". Given that, I would not expect to be exceeding 10 seconds except when given an exceedingly large data set. Your file I/O may even be the bottleneck. This to me suggests that you could just abort straight away if the file to load in is above a certain size, assuming you are using such an algorithm.

    I guess though that it depends on what your reason for the 10 second cutoff is. Care to fill us in?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    Optimial topoligical sorting algorithms run indeed in linear time pretty much. However, I coded my own solution, which is sub-optimal and run in nē time. I basically create an adjacency matrix and traverse it to find the topological order.

    That is why with very large datasets my program gets slow.

    I sure will implement a more efficient algorithm asap, but I figured it could be cool to learn how to abort one as well.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I use timers a good deal, but not this kind. This works, so give it a try.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void) {
       int i;
       char a[7][128];
       time_t start, stop;
       FILE *fp=fopen("Edelweiss.txt", "r");
       if(!fp) {printf("\nError: unable to open the file.\n"); return 1;}
        start = time(NULL);
    
       i=0;
       while((fgets(a[i],sizeof(a[i]), fp)) != NULL) {
          fputs(a[i++],stdout);
          getchar();
       }
       fclose(fp);
       stop = time(NULL);
       if(stop-start > 10) { 
          printf("\nYour 10 seconds are up! Ending program in 2 seconds!\n");
          _sleep(2);
       }else {
          printf("\nYou were fast! The program will end when you hit enter\n");
          getchar();
       }
       return 0;
    }
    /*
    In a file named "Edelweiss.txt, put this into the programs directory before you run it:
          "Edelweiss, edelweiss, every morning you greet me.
          Small and white, clean and bright, you look happy to meet me.
          Blossom of snow may you bloom and grow, bloom and grow forever.
          Edelweiss, edelweiss, bless my homeland forever.
          Small and white, clean and bright, you look happy to meet me
          Blossom of snow, may you bloom and grow, bloom and grow forever.
          Edelweiss, edelweiss, bless my homeland forever."
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  2. Timing C program always returns 0 seconds.
    By hamsteroid in forum C Programming
    Replies: 6
    Last Post: 03-24-2007, 10:02 AM
  3. Time to seconds program
    By Sure in forum C Programming
    Replies: 1
    Last Post: 06-13-2005, 08:08 PM
  4. Pauseing a program for 3 seconds
    By Queatrix in forum C++ Programming
    Replies: 17
    Last Post: 04-09-2005, 07:35 PM
  5. Replies: 0
    Last Post: 04-27-2003, 02:04 AM