# Thread: how to use the clock() function?

1. ## how to use the clock() function?

hey i got to write a program which stores randoms numbers in an array use insertion sort and selection sort and time both of them witht the lcock fnction to see which is faster.how do i use the clock function?? where do i put it?
Code:
```#include<stdio.h>
#include<stdlib.h>
#include<time.h>

void print(int[]);
int random(int,int);
void insertionsort(int[]);
void selectionSort(int[]);
int main(){
int i,arr1[10000],arr2[10000];
srand(time(0));
for(i=0;i<10000;i++){
arr1[i]=random(1,1000000);
arr2[i]=random(1,1000000);
}

insertionsort(arr1);
print(arr1);
selectionSort(arr2);
print(arr2);
system("pause");
return 0;
}
int random(int m,int n){
int offset =rand()/(RAND_MAX + 1.0)*(n-m+1);
return m+offset;
}

void insertionsort(int arr[]){
int j,k,key;
for(j=0;j<10000;j++){
key=arr[j];
k=j-1;

while(k>=0 && key<arr[k]){
arr[k+1]=arr[k];
--k;
}
arr[k+1]=key;
}
}

void selectionSort(int numbers[])
{
int array_size=10000;
int i, j;
int min, temp;

for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}

void print(int arr[]){
int j;
for(j=0;j<10000;j++){
printf("%d  ",arr[j]);
}
}```

2. Code:
```#include <clock.h>
....

clock_t t;
...
t = clock();
// do something that takes a while here.
t = clock() - t;  // calculate difference from start to now.
...
printf("%f", (double) t / CLOCKS_PER_SEC);```
--
Mats

3. Ran into a problem with clock the way you have it in that if you need to do some nul cycles using it's possible that clock will not increment. In which case you need to use time in which case you can't get tenths of a second. It was in my Go-Cart program that I ran into that problem.