13,592,218 members
alternative version

#### Stats

62K views
18 bookmarked
Posted 3 Mar 2003
Licenced

# Timing sort algorithm

, 3 Mar 2003
Timing fluctuation can limit the usefulness of timing experiments.

## Introduction

Timing fluctuation can limit the usefulness of timing experiments. The idea here is to place the algorithm inside a loop and count the number of iterations between ticks.

The reason for using the `time` function rather then the `clock` function is because it can be poorly implemented with the result of "missing ticks", and accumulating this over many loops, it will lead to inaccuracy up to 50%. Using the `time` function and looping in between ticks will decrease the inaccuracy.

## Example

```time_t start;
start = time(NULL);```
```vector<long> iterations;

//reps is the nunber of time that the code will loop
iterations.reserve(reps);

while(iterations.size() < reps){
int count = 0;
do{
count++;
copy(A, A+N, B);
sort(B, B+N);
//getting end time
finish = time(NULL);
//check that the time has changed before
//going back into loop and increment count
}while(start == finish);
//inserting count value in the vector that indicates the time we
//have looped between a tick during time function
iterations.push_back(count);
start = finish;
}```

This can then be repeated for several times, so even if the duration between ticks vary, we can select the most accurate.

## Output

The output below shows the average result that has been calculated sorting an array of `double`s of length 10000 for 10 times.

The result is calculated by retrieving the middle value after the vector has been sorted and dividing it by 1000.0 which will give us the milliseconds.

`1000.0/iterations[reps/2]`

A list of licenses authors might use can be found here

## Share

 Web Developer United Kingdom
No Biography provided

## You may also be interested in...

 First Prev Next
 Time intro Pesto5-Mar-03 5:05 Pesto 5-Mar-03 5:05
 Choice of timer Rick York4-Mar-03 6:33 Rick York 4-Mar-03 6:33
 Re: Choice of timer Emiliano4-Mar-03 6:57 Emiliano 4-Mar-03 6:57
 Re: Choice of timer - CPUTicker Neville Franks5-Mar-03 15:10 Neville Franks 5-Mar-03 15:10
 Time is the simplest thing... peterchen4-Mar-03 3:31 peterchen 4-Mar-03 3:31
 Re: Time is the simplest thing... dog_spawn4-Mar-03 9:39 dog_spawn 4-Mar-03 9:39
 Hm... Chopper4-Mar-03 3:24 Chopper 4-Mar-03 3:24
 Last Visit: 31-Dec-99 18:00     Last Update: 20-Jun-18 0:14 Refresh 1