Click here to Skip to main content
12,397,059 members (62,842 online)
Click here to Skip to main content
Add your own
alternative version

Stats

58.8K views
489 downloads
18 bookmarked
Posted

Timing sort algorithm

, 3 Mar 2003
Rate this:
Please Sign up or sign in to vote.
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 doubles of length 10000 for 10 times.

Sample Image - timing_sort_algorithm.gif

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]

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Emiliano
Web Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralTime intro Pin
Pesto5-Mar-03 5:05
memberPesto5-Mar-03 5:05 
GeneralChoice of timer Pin
Rick York4-Mar-03 6:33
memberRick York4-Mar-03 6:33 
GeneralRe: Choice of timer Pin
Emiliano4-Mar-03 6:57
memberEmiliano4-Mar-03 6:57 
GeneralRe: Choice of timer - CPUTicker Pin
Neville Franks5-Mar-03 15:10
memberNeville Franks5-Mar-03 15:10 
GeneralTime is the simplest thing... Pin
peterchen4-Mar-03 3:31
memberpeterchen4-Mar-03 3:31 
GeneralRe: Time is the simplest thing... Pin
dog_spawn4-Mar-03 9:39
memberdog_spawn4-Mar-03 9:39 
GeneralHm... Pin
Chopper4-Mar-03 3:24
memberChopper4-Mar-03 3:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 4 Mar 2003
Article Copyright 2003 by Emiliano
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid