Thread: Problem woth CLK_TCK

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    25

    Problem woth CLK_TCK

    I'm supposed to do a sequential earch on integers and also retun the time elapsed on doing such a search . Here is what I have.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<time.h>
     void selection(int a[] , int n);
     void main()
     {
      int a[20],i,n;
     clrscr();
     printf("enter the no. of elemnts ");
     scanf("%d",&n);
     printf("enter elemnts");
      for(i=0;i<n;i++)
     {
      scanf("%d",&a[i]);
    }
      selection(a,n);
      getch();
    }
    void selection ( int a[], int n)
     {
      int i,j,temp,min;
      clock_t st,end;
       st= clock();
        for(i=0;i<=n-2;i++)
         {
           min=i;
            for(j=i+1;j<=n-1;j++)
              {
                 if( a[j]<a[min])  min=j;
               }
                 temp= a[i];
                 a[i]= a[min];
                 a[min]=temp;
            }
          end= clock();
       printf("sorted array is ....");
       for(i=0;i<n;i++){
       printf("%d",&a[i]);}
      printf("elapsed time is ..%f",(end-st)/CLK_TCK);
     }
    While the sorting works okay the time elapsed is always 0.00000. why? and how to make it okay?
    Last edited by ramayana; 11-03-2006 at 11:44 AM.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Probably because the resolution of the timer is not accurate enough. You might do better with a high resolution timer but I believe they are platform specific, eg QueryPerformanceCounter(windows) or gettimeofday.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    25
    Can it be because I'm using float and not double and beacuse the time elapsed is small enough that it doesnt fit onto the float format?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    do the averages print out properly? this doesnt even compile on my computer, and when i got it to, the average was always a string of about 15 numbers, after the 'average' prints out i get a runtime error. is your sort flawed?

  5. #5
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Quote Originally Posted by ramayana
    Can it be because I'm using float and not double and beacuse the time elapsed is small enough that it doesnt fit onto the float format?
    I doubt it but you could always try it. I don't think you'll get better than millisecond precision with clock().

    While not directly related to your question, you should understand that main returns int.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >why?
    Because 1) it takes less than one second to do what you want and your calculation is integer based (which truncates precision) and 2) the sort is so fast with that number of elements that even if you were using floating-point math in your calculation, you would have to run the sort many times just to get a reading. I had to increase the size of the array to 10,000 before the time even registered at that precision:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void selection ( int a[], int n )
    {
      int i, j;
      int min;
      int temp;
    
      for ( i = 0; i <= n - 2; i++ ) {
        min = i;
    
        for ( j = i + 1; j <= n - 1; j++ ) {
          if ( a[j] < a[min] )
            min =j ;
        }
    
        temp = a[i];
        a[i] = a[min];
        a[min] = temp;
      }
    }
    
    #define N 10000
    
    int main ( void )
    {
      clock_t start;
      int *a = malloc ( N * sizeof *a );
      int i;
    
      for ( i = 0; i < N; i++ )
        a[i] = rand();
    
      start = clock();
      selection ( a, N );
      printf ( "Elapsed time is ..%f\n", (clock() - start) / (double)CLOCKS_PER_SEC );
    
      free ( a );
    
      return 0;
    }
    p.s. CLK_TCK is not standard. The standard equivalent is CLOCKS_PER_SEC.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM