test my program locally for multiple test cases

how can i test my program locally for multiple test cases without submitting it. For eg. finding the time taken by my program for multiple cases so that it can be optimized further to run in time given in the problem.
i hope that i am able to express my problem.
thanks.

You can write your own input generator and test it on them multiple times. And when you working on Linux, there is simple command time which measure time consumed by your program. In Linux is also pretty easy to set memory limit (although I don’t know exact command).

But problem is, that such things are not very reliable. Because you don’t know on which machine your program will be tested. The testing computer can be faster or slower than your local computer. But you can still estimate running time of your program. For that we use time complexity and O-notacion. And in consideration of input size you know if your program is fast enough.

For example, if in statement n<=10^5 you know, that solution with time complexity O(n log n) will be sufficient, but program O(n^2) is too slow. Second estimation you can use is, that computer can do about one bilion operations in one second. But of course that is just rough estimation.

So the best way (and I believe the most used) is to calculate time complexity of your solution and together with input size see if it’s sufficient.

If I understood you well, you want to know that when you got TLE, how long was your program running to know if you have to do it 2 times quicker or 5 times quicker or so?

It is not possible. If there is time limit set to 1s and your code runs for 1.1s or so, judge kills the the process and returns TLE, it’s not waiting any more…

1 Like

i was submitting my solution for TSORT Problem - CodeChef
this problem and it is saying TLE , time limit is 5 sec
here is my solution

#include<stdio.h>

inline void fastread(int*);

int main()

{

    int min,i,j,n;
    
    int *a;
    scanf("%d",&n);
    a = (int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++){
        fastread(&a[i]);
    }

    for(i=0;i<n;i++)
    {
         min=a[i];
         int loc=i;
        for(j=i;j<n;j++)
        {

            if(min>a[j])
            {
                min=a[j];
                loc=j;

            }
        }
        a[loc]=a[i];//--------swap is in outer loop O(n) swaps therefore better than insertion sort in terms of swaping------------
        a[i]=min;
    }
    for(i=0;i<n;i++)
    {
        printf("%d\n",a[i]);
    }
    return 0;
}

inline void fastread(int*a)

{

   register char c=0;

   while (c<33) c=getchar();

   *a =0;

   while(c>33)

   {

       *a = *a*10+c-'0';
    
       c=getchar();
   }

}

obviously it would be taking more than 5 sec to run all test cases that’s why it is showing TLE
i just want to know how much time it is taking
and till how many test cases it is running within 5 sec.
hope i am clear now.

time is precisely taken as the sume of time taken by loops in ur code
so just add the number of times each loop runs n see ur time

thanks for your reply
but i want to know that if i am getting TLE in any submission than how much time it is taking actually for all the test cases and till what input it is working fine so that i can optimize it further.

sorry, but I really don’t understand your question

sorry that i am unable to express.
I was submitting a solution to a problem and it was showing time limit exceeded that means it is taking more time than mentioned in the problem statement.
i just want to know that how much time(in seconds) it is taking for all the input cases of codechef.
i hope that i am clear now.

Well if your algorithm is good enough it will work for all cases, you would be giving yourself much more work if you wanted to use this approach often, for the easy and medium problem I advise you to find an algorithm that works for the edge cases specially those with large inputs and that work on the constraints… For that as @michal27 said you should write an input generator and you have to make sure your reading and writing routines are fast enough, there are posts and pages here on codechef that cover that part, if you want to check for yourself just submit a solution to INTEST.

@nishant_25: I don’t think you need to know how much time it takes. If you had TLE, than there are two possibilities:

  1. Your solution is just slow from its concept - that can be verify by time complexity. As I mentioned, if you try submit O(n^2) solution with n=10^5 you never get it right with any optimization.

  2. Your idea is fast enough, you just implementid slowly. In this case there are several approaches you can try - check if your I/O is fast enough, if you don’t use double arithmetic instead integer etc.

1 Like

Well, this may not be a true. At least Slovak judge wait to max(2.time, time+2) where time is timelimit. And I don’t think that has no reason.

So sometimes it’s possible see, that my program run 1.2 seconds when timelimit is 1s. But probably codechef doesn’t support this feature.

I love such codes - you used fast read, but slow sorting O(n^2)… You have to use O(n * log(n) ) sorting :wink:

probably even O(n) counting sort. Checkout this question

yes that is exactly what i want to know somehow

yes the sorting(selection sort) i used is slow.
i am just giving an example.

Aaa, I see. You cannot find this information, because someone can print the result at the end instead of during the processing. Information about how many bytes were written to stdout is not available…

suppose i am having a O(nlogn) solution with n=10^5.
than how can i find time(in seconds) taken by solution for n=10^5 or for any other n. Is it possible

You cannot, as @michal27 wrote

you don’t know on which machine your program will be tested

can i find it for my own system or for my own machine?

@michal27 Your code ran for 1.2 seconds on a problem with 1 sec time limit, because, there were more than one test file, and the time reported is the total time taken. The time limit, however, is for individual test files.