Thread: Execution time problem.

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    1

    Question Execution time problem.

    We need to sum n numbers using for,while, and do while loops..
    and need to compare the execution time for each of them.
    I wrote the following code.
    but every time the output comes out to be different!!
    so not able to decide which loop executes the fastest.
    kindly help me figure this out..

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<time.h>
    #include<dos.h>
    #define n 20000
    int sumfor();
    int sumwhile();
    int sumdowhile();
    
    
    int a[n]; 
    time_t first,second;
    double tfor,twhile,tdowhile;
    void main()
    {
    int i,s1,s2,s3;
    
    
    // initializing the array.. 
    for(i=0;i<n;i++)
    {
       a[i]=1;
      }
    }
    
    
    
    
    
    
    s1=sumfor();
    s2=sumwhile();
    s3=sumdowhile();
    
    
    if ((s1==s2) && (s2==s3))
     printf("\nsum is %d",s1);
    else
    printf("\nerror!!!");
    
    
    
    printf("\ntime with for loop %f",tfor);
    
    
    printf("\ntime with while loop %f",twhile);
    
    
    printf("\ntime with do-while loop %f",tdowhile);
    
    
    getch();
    }
    
    
    int sumfor()
    { int i,p=0;
    
    
    printf("\nRunning for loop");
    first=time(NULL);
    
    
    for(i=0;i<n;i++)
    {
     p=p+a[i];
    printf("\nadding %d th element",i);
    }
    
    
    second=time(NULL);
    
    
    tfor=difftime(second,first);
    
    
    
    
    return p;
    }
    
    
    int sumwhile()
    {
    int i,p;
    i=0;
    p=0;
    
    
    printf("\nRunning for while loop");
    first=time(NULL);
    
    
    while(i<n)
    {
     p=p+a[i];
      printf("\n adding %d th element",i);
    i++;
    
    
    }
    second=time(NULL);
    twhile=difftime(second,first);
    return p;
    }
    
    
    int sumdowhile()
    {
    int i,p;
    i=0;
    p=0;
      
    printf("\nRunning for do while loop");
    
    
    first=time(NULL);
    do
    {
    p=p+a[i];
    i++;
     printf("\n adding %d th element",i);
    }while(i<n);
    second=time(NULL);
    tdowhile=difftime(second,first);
    return p;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You have the main returning void.Also the parses do not seem correct to me.I think the the } in line 25 is wrong.

    However it is not clear to me.Your calculations result in a different result with each loop?

    EDIT->in the do while you increase value of i and then print,so it says that you added 1st element and finally the 200-th element,but in the other two(for,while) you start from 0-th element and end to 199-th element.Maybe you want to correct it.

    I got that the for loop is faster.If they have they same time for you,you may increase value of n
    Last edited by std10093; 08-27-2012 at 10:30 AM.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Presumably your teacher told you how to do the timing, and presumably it is not the way you're doing it. The problems with your approach is that it only has one-second resolution and that it measures wall-clock time which makes it useless.

    Are you on a *nix system? (Linux or BSD, for instance?) If so, try the "time" command:

    $ time your_program


    And as std10093 has pointed out, your program as posted will not even compile because of mismatched braces. E.g.,
    Code:
    for(i=0;i<n;i++)
    {
       a[i]=1;
      }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is there a reason you've posted C on the C++ board?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Moved to C board.

    A couple of points.

    1. Your indentation needs work.
    SourceForge.net: Indentation - cpwiki

    2. printf("\n adding %d th element",i);
    This will seriously screw up the timings.
    Summing 20000 integers (in any method) will take no time at all in comparison to the time it takes to do all those printf calls.

    To generate something like meaningful times when all you have is a slow stopwatch, you need to do something like
    Code:
    t1 = time()
    for ( i = 0 ; i < 1000 ; i++ ) {
        for ( j = 0 ; j < n ; j++ ) {
            // test code
        }
    }
    t2 = time()
    result = difftime(t2,t2) / 1000;
    The outer loop runs the test code 1000 times.
    Then we divide the total time by 1000 to get an average for just one iteration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    for highest resolution timing, in linux use clock_gettime with the monotonic option. in windows, use QueryPerformanceCounter and QueryPerformance Frequency

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculate Execution Time?
    By infantheartlyje in forum C Programming
    Replies: 11
    Last Post: 10-14-2011, 02:01 PM
  2. Comparing time taken for execution
    By AvaGodess in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 02:15 PM
  3. execution time
    By shuo in forum C++ Programming
    Replies: 3
    Last Post: 10-17-2007, 02:58 AM
  4. Execution Time in Microseconds?
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2007, 01:32 PM
  5. execution time in C , Linux
    By vinayonnet in forum C Programming
    Replies: 4
    Last Post: 06-21-2006, 01:06 PM