How is Time calculated for a program.

Does the time required by my program also includes the pre-computation part. Obviously I do it before any inputs.

yes of course, it(time required) includes every thing like any loop for initialization of an array,for scanning and also even for printing.

1 Like

Time required starts is calculated after the first input,isn’t it like that?
Cuz I’ve submitted solutions based on this “assumption”.

no i already mentioned that it starts from the beginning of your program

The time taken by the program is the time interval that starts from the execution of the first statement in main() to the last statement in main().

You can do it yourself,


Put this code at the starting of main (before all the statements)

clock_t t1,t2;

t1=clock();


Put this code at the end of main( after all statements but **before return 0;**)
**t2=clock();**

cout << “the time to execute is:” << (float)(t2-t1)/CLK_TCK << endl;

This time will depend mainly on the processor used. The time shown by your system might differ from that shown by the judge here as they might be using a different processor.

The time taken by these processor might be more , less or same as compared to your processor.

include the header or <time.h>

In JAVA, you can use the System.nanoTime() method at the beginning of main method and at the end of the main method, and just display the difference.
Example is shown below-

class Example
{
    public static void main(String args[])
    {
        long t1 = System.nanoTime();
        /*      
                 Your Code
        */
        long t2 = System.nanoTime();
        long timeTaken = t2 - t1;
        // timeTaken is the time taken for the processor for the execution of program.
    }
}
1 Like