CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2012
    Posts
    1

    Question C++: I can't properly get the processing time for this program. Please help. clock_t?

    This is a code which sorts arrays.

    I use clock_t to determine the processing time for my code. However, it always stacks with the time taken from before. It looks like it's just adding the results.

    Code:
    int main(int argc, char *argv[])
    {unsigned long n;//size of array -- long for big numbers of n
    int choice, choice2, sort;//numbers corresponding to the choices(switch)
    clock_t Begin, End;
    float elapTicks, elapMilli;
    wrongchoice://goto function -- bringing back to this point after entering wrong choice
    cout<<"===============================…
    cout<<"\t\t\tChoose the order of the output:\n\t\t\t\t[1] Random-Descending\n\t\t\t\t[2] Ascending-Descending\n\t\t\t\t[3] Descending-Ascending\n\t\t\t\t[4] Random-Ascending\n\t\t\tChoice: ";
    cin>>choice;
    cout<<"\t\t\tIndicate size of array: ";
    cin>>n;
    long int array[n];//first and original array to be used all through the program
    long int arrayb[n];//second/temporary array used to preserve/copy original values of first array
    for (int i=0; i<n; i++)
    {array[i]=rand()%99999;
    arrayb[i]=array[i];//copy value of the first array into the second array
    }
    double temptime=0;
    clock_t realend, realend2, realend3, realend4, realend5, realend6;
    sorting:
    cout<<"\n\t\t\t\t[1] Bubble Sort\n\t\t\t\t[2] Selection Sort\n\t\t\t\t[3] Insertion Sort\n\t\t\t\t[4] Quick Sort\n\t\t\t\t[5] Shell Sort\n\t\t\t\t[6] Merge Sort\n";
    cout<<"\t\t\tChoose what sorting algorithm to use: ";
    cin>>sort;
    goto copyarray;//goto function to copy array elements(located at line 747)
    sorting2://goto function -- used to resume back to this point after copying array elements
    srand(time(NULL));
    switch(choice)//first switch -- indicates the desired order of the output
    {
    case 1://Random-Descending
    {cout<<"==============================…
    cout<<"\t\t\tUnsorted Array: "<<endl;
    for(int i=0; i<n; i++)
    cout<<array[i]<<"\t";
    cout<<"\n\n\t\t\tSorted Array:\n";
    switch(sort)//second swtich -- indicates what sorting algorithm to use
    {
    case 1:
    {//Bubble Sort Descending
    clock_t Begin1, End1;
    realend=clock();
    Begin1=clock();//indicates start of timer/clock counter(every start of cases for sorting)
    BubbleSort2(array, n);//calling of function
    End1=clock();//indicates end of timer/clock
    float time1;
    time1= (End1-Begin1/1000.00f);//used to convert value(taken from the clock code) to milliseconds(from seconds)
    cout<<"Bubble Sort:"<<endl;
    for (int i=0; i<n; i++)
    cout<<array[i]<<"\t";
    cout<<"\n\nTotal operational count is: "<<opcount<<endl;
    cout<<"\nAlgorithm Runtime is: "<<time1<<" milliseconds.";
    cout<<endl<<endl;
    goto choose;//goto function to indicate what to do after sorting
    break;}
    case 2:
    {//Selection Sort Descending
    clock_t Begin2, End2;
    realend2=clock();
    Begin2=clock();
    double temptime2=Begin2;
    SelectionSort2(array, n);//calling of function
    End2=clock();
    float time2;
    time2 = (End2-Begin2/1000.00f);
    cout<<"Selection Sort:"<<endl;
    for (int i=0; i<n; i++)
    cout<<array[i]<<"\t";
    cout<<"\n\nTotal operational count is: "<<opcount<<endl;
    cout<<"\nAlgorithm Runtime is: "<<time2<<" milliseconds.";
    cout<<endl<<endl;
    goto choose;//goto function to indicate what to do after sorting
    break;}
    That is some part of my code. I couldn't put it all here since it's too long. The time adds up with the time that is first taken when I choose another option from the switch code, even though I declared a new variable for clock_t. Please help T_T I really don't know what's wrong with the code.

    Here's the entire code:
    http://www.daniweb.com/software-deve...48#post1775548



    Feel free to paste it in any c++ debugger so you would understand the code better.

    I hope someone can help me.

  2. #2
    Join Date
    Jul 2005
    Posts
    19

    Re: C++: I can't properly get the processing time for this program. Please help. cloc

    Sorry, I am replying without checking code. I would suggest use the systime to have the clock value of the system. In the end print the differences of start time and end time.

    regards,
    Vatsa
    www.objectiveprogramming.com

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: C++: I can't properly get the processing time for this program. Please help. cloc

    Quote Originally Posted by minxminx View Post
    I hope someone can help me.
    Your chances of getting help will increase drastically if you properly format your code and get rid of the goto's.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C++: I can't properly get the processing time for this program. Please help. cloc

    Yes, please format the code. Most IDEs have ways of doing it for you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured