Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,916 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,107 people online right now. Registration is fast and FREE... Join Now!




Problem with ctime

 
Reply to this topicStart new topic

Problem with ctime

dmd1120
5 Jan, 2008 - 03:24 PM
Post #1

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 6


My Contributions
Hello, this is my first time posting here on DreamInCode. I am so glad that I ran across this site, as it is a great resource!

I am trying to write a program that can help me to see the difference in processing time between using an insertion sort, a quicksort, and a hybrid of the two. I have it all done except for the fact that when I output the start and ending times, it shows up as a negative number, with 0 processing time. I don't think this is right, as I am sorting an array of 100,000 integers. I will include a portion of my code here. Please let me know if there is any more info that you need in order to help me. However, I think it's just a matter of my not having experience in using the ctime class.

I would greatly appreciate if you could tell me what I'm doing wrong here, or how I can get it to display the processing time in seconds or milliseconds.
CODE

int main (void)
{
//    Local Declarations
    int i;
    int ary[MAX_ARY_SIZE];
    time_t start;
    time_t complete;

//    Statements

    fillArray(ary);

    ctime(&start);
    
    quickSort (ary, 0, MAX_ARY_SIZE - 1);
    
    ctime(&complete);

    cout << "Sorting Started at " << start << endl;
    cout << "Sorting Complete at " << complete << endl;
    cout << "Total time to sort: " << complete - start << endl;

    cout << endl;
    return 0;
}    // main


User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Problem With Ctime
5 Jan, 2008 - 03:44 PM
Post #2

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,589



Thanked: 12 times
Dream Kudos: 325
My Contributions
I'm not an expert in C++, but the ctime function (according to the docs) converts a time_t structure and returns it as a string.

I think you should be using the time function as described here here.

Something like this.
CODE


int main (void)
{
//    Local Declarations
    int i;
    int ary[MAX_ARY_SIZE];
    time_t start;
    time_t complete;
    time_t delta;

//    Statements

    fillArray(ary);

    start = time(NULL);
    
    quickSort (ary, 0, MAX_ARY_SIZE - 1);
    
    complete = time(NULL);
    delta = difftime(start, complete);

    cout << "Sorting Started at " << ctime(&start) << endl;
    cout << "Sorting Complete at " << strftime(&complete) << endl;
    cout << "Total time to sort: " << strftime(&delta) << endl;

    cout << endl;
    return 0;
}    // main


I don't know if that is completely right though... you might be better off using a timer with better precision, as the times you're measuring are going to be pretty small.
User is online!Profile CardPM
+Quote Post

jjhaag
RE: Problem With Ctime
5 Jan, 2008 - 04:07 PM
Post #3

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Tom9729 is right about ctime - that's basically just a formatting function, not a timer.

I agree that you'd be better off using a more precise timer - for instance, clock() instead of time(), because of the added precision it gives you. clock() returns the number of "clock ticks" since the program started - and you can convert to seconds using the macro expression CLOCKS_PER_SEC. So the pertinent sections of the code would be:
CODE
long start;
long complete;
//...
start = clock();

quickSort (ary, 0, MAX_ARY_SIZE - 1);

complete = clock();

double elapsed_time = double(compete-start)/double(CLOCKS_PER_SEC);

cout << "Total time to sort: " << elapsed_time << endl;

User is offlineProfile CardPM
+Quote Post

dmd1120
RE: Problem With Ctime
5 Jan, 2008 - 04:25 PM
Post #4

New D.I.C Head
*

Joined: 21 Nov, 2007
Posts: 6


My Contributions
QUOTE(jjhaag @ 5 Jan, 2008 - 05:07 PM) *

Tom9729 is right about ctime - that's basically just a formatting function, not a timer.

I agree that you'd be better off using a more precise timer - for instance, clock() instead of time(), because of the added precision it gives you.



Thank you, Tom and JJ for your help. There are just so many different classes available in the STL, and I haven't learned many of them yet. I was able to get the time functions to work, but as you both have said, I may be better off using the timer. However, I have another question... is it possible to find out how many milliseconds have passed? The largest amount of time it takes for a full insertion sort to work on 100,000 integers was only about 31 seconds.. so I think I need more precision for the assignment. (The assignment is to create a chart that shows the results of writing the hybrid sort, and changing the minimum number for it to go into the insertion sort. Obviously, it's difficult to notice much of a difference when the longest time is only 31 seconds.

EDIT: Well, the two of you pointed me in the right direction, because I just did a search here in the C++ forum on 'millisecond', and found a thread from someone who appears to have had a similar assignment. born2c0de had brought up another function, GetTickCount, which is part of the windows.h class, and that actually returns the milliseconds for me. Perfect! Thank you again for all your help, and for leading me the right way. biggrin.gif

This post has been edited by dmd1120: 5 Jan, 2008 - 04:44 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 02:28PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month