time of execution

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nikhil124
    New Member
    • Jan 2010
    • 6

    time of execution

    I've written this C prog. (insertion sort) which sorts and finds the time of execution.

    #include<stdio. h>
    #include<conio. h>
    #include<time.h >

    main()
    {
    int a[25] , i , j , k;
    clock_t start,end;
    clrscr();
    printf("\n enter the numbers ");
    for( i= 0 ; i <= 24 ; i++)

    scanf("%d" , &a[i]) ;
    start=clock();
    for(i =0 ; i<= 24 ; i++ )
    {
    for( j= 1 ; j<= 24 ; j++ )
    {
    if(a[i]> a[j])
    {
    k = a[i];
    a[i]=a[j];
    a[j] = k ;
    }
    }
    }

    printf("\n sorted numbers are ");
    for(i = 0 ; i<= 24 ; i++ )
    {
    printf("%d" , a[i]) ;
    }
    end=clock();
    printf("\n\nTim e of execution : %f",(end-start)/CLK_TCK);
    getch();
    }
    ---------------
    my problem is that it always outputs time of execution = 0.000000 whatever i do.
    why it is not showing different values ? what changes should i make to this prog?is there any other way to find time of execution ?
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Not sure. Try casting to a double before dividing:
    ((double)(end-start))/ CLOCKS_PER_SEC) ;

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      From the C Standard

      The clock function returns the implementation' s best approximation to the processor time used by the program since the beginning of an implementation-defined era related only to the program invocation. To determine the time in seconds, the value returned by the clock function should be divided by the value of the macro CLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the function returns the value (clock_t)-1.

      Notice that last sentence. The C Standard does not guarantee that this function accomplishes anything. Check if clock is returning -1. If so, then you need to look for another function, probably not a standard function, to measure elapsed time with.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Your program is too fast. Try executing the program, say 100,000 times, in a loop. Do your clock before and after the loop and divide the difference in times by 100,000.

        Comment

        • RedSon
          Recognized Expert Expert
          • Jan 2007
          • 4980

          #5
          You are only sorting 24 numbers? Even something like bubble sort would sort that almost instantly. Worst case performance of that is O(n^2) and if n = 24 thats nothing for a desktop machine. Try sorting a million items or something like that then you will see how long it takes.

          Comment

          Working...