How to find the Execution time?

Discussion in 'C' started by Pradeep.M, Mar 29, 2012.

  1. Pradeep.M

    Pradeep.M New Member

    Joined:
    Feb 1, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India
    Hey friends,

    Could you please help me, by telling how could I find the execution time of a simple C/C++ program I write. I usually use DevC++/Visual studio 2008, but in different competitions they check my programs execution time and I often get rejected even if my prog. is correct. Please tell me a software to check the execution time myself.

    Thank you
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How long do the programs run for and what accuracy is needed?

    Be aware that hardware differences could explain this. If your overclocked 5GHz i7 runs the program in 0.2ns, and their 100MHz Pentium 2 takes a week to crunch its way through the program, maybe that's the problem.

    One easy way if your program runs from the command line is to use "echo .|time". For example, if I copy and paste the following into a Command Prompt:

    echo.|time
    dir
    echo.|time

    I get the following results:

    D:\MyProgs>echo.|time
    The current time is: 7:44:59.48
    Enter the new time:

    D:\MyProgs>dir
    Volume in drive D has no label.
    Volume Serial Number is 1020-1853

    Directory of D:\MyProgs

    16/03/2012 18:21 <DIR> .
    16/03/2012 18:21 <DIR> ..
    06/07/2011 15:13 <DIR> bios_img
    23/10/2010 10:55 <DIR> DeSmuME
    20/06/2011 15:00 <DIR> dfhack-0.5.15
    20/06/2011 15:00 1,131,684 dfhack-0.5.15.zip
    20/03/2012 16:06 <DIR> Dwarf Fortress
    18/03/2011 09:22 <DIR> DwarfTherapist-0.6.10
    20/11/2010 01:05 <DIR> Mandelbulb3Dv166
    05/03/2012 08:53 <DIR> Process Explorer
    14/11/2011 13:54 <DIR> Process Monitor
    14/11/2011 12:28 <DIR> PSTools
    18/08/2009 20:41 <DIR> Quake2
    27/08/2010 17:48 <DIR> Soko
    16/03/2012 18:21 <DIR> sqldeveloper
    12/03/2012 23:52 <DIR> SunVox 1.6.4
    08/07/2010 11:38 <DIR> TathamPuzzles_2010-07
    19/08/2011 17:49 <DIR> VirtualDub
    07/01/2012 10:45 <DIR> WD Data Lifeguard
    09/03/2012 08:49 <DIR> WinVICE-2.2-x64
    11/11/2010 10:30 <DIR> WinVICE-2.2-x86
    30/07/2010 17:20 <DIR> xlogical-1.0
    1 File(s) 1,131,684 bytes
    21 Dir(s) 106,429,923,328 bytes free

    D:\MyProgs>echo.|time
    The current time is: 7:44:59.51
    Enter the new time:

    D:\MyProgs>


    so you can see the runtime was 7:44:59.51-7:44:59.48 = 0.03 seconds. Note also that caching can affect the result; if I repeat dir 10 times then I get a runtime of 0.16 seconds instead, which is NOT 10*0.03.

    If your program generates tons of output and you lose the start time, try instead:

    echo.|time>start.txt
    dir/s
    echo.|time>stop.txt

    This gives me:
    D:\MyProgs>type start.txt
    The current time is: 7:50:58.22
    Enter the new time:

    D:\MyProgs>type stop.txt
    The current time is: 7:51:05.93
    Enter the new time:

    for a total dir/s runtime of 7:51:05.93-7:50:58.22=7.71 sec
     
  3. Pradeep.M

    Pradeep.M New Member

    Joined:
    Feb 1, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India
    Could you please name me a simple software to check the execution time. Now when I say execution time, I myself am aware abt the processor speed and the various instruction formats used today in latest processor, but I want to check the time (execution) when runned on the codechef server or topcoder servers.
    Well you would say, how could one know what processor they use, but, see, u submit ur code on codechef or topcoder or any other competition servers, they would tell u ur execution time, and its same for all, so they must use a boundary or specific code testing processor speed platform, so there must be a specific software or something like that, and all I want is the name of that software or the process.

    Obviosly they don't run these 2-3 line code on each program, think, world-wide, so many problems are being submitted at a time, and its not possible to compute all of them at once. So I assume they just feed the code into some software and they get the time,execution,runtime, SIGSEV and all that....

    I hope I explained it well to u !!!
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    I just did name some simple software (time), and you didn't answer the questions I asked about duration and accuracy.

    Why not ask at those sites how they time the software? Or have a look at the FAQ and see if there is, for example, a question like "How can I measure the execution time of my program locally?" (wording lifted directly from one of the sites you named.)
     
  5. Pradeep.M

    Pradeep.M New Member

    Joined:
    Feb 1, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India
    Found it in codechef FAQ....

    But I cannot understand nicely, that is y I posted the question on this forum. If you know the answer, please give me a bit of detailed explanation once more..
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How long do the programs run for and what accuracy is needed?

    Or, to put it another way, why can't you use the method I suggested?
     
  7. Pradeep.M

    Pradeep.M New Member

    Joined:
    Feb 1, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    India
    Ok !!.. Sir.. I would go with ur method. then tell me how would I calculate:-

    Say I have written a simple program Hello.c

    #include<stdio.h.>
    int main()
    {
    printf("Hello, how are you ");
    return 0;
    }


    now what would I do.. suppose I have saved it in D:\Prog\Hello.c
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice