Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C++中打印一个实例的运行时间_C++_Algorithm_Sorting_Time_Clock - Fatal编程技术网

如何在C++中打印一个实例的运行时间

如何在C++中打印一个实例的运行时间,c++,algorithm,sorting,time,clock,C++,Algorithm,Sorting,Time,Clock,我有一个包含5种排序算法的程序:气泡排序、选择排序等。用户被要求选择他想要使用的算法,然后被要求写下他想要排序的数字数量,这些数字是随机生成的。我的问题是如何打印每个案例完成所需的时间 提前谢谢 代码如下: #include <iostream> #include <stdio.h> #include <stdlib.h> #include<string> #include <ctime> #include <cstdlib>

我有一个包含5种排序算法的程序:气泡排序、选择排序等。用户被要求选择他想要使用的算法,然后被要求写下他想要排序的数字数量,这些数字是随机生成的。我的问题是如何打印每个案例完成所需的时间

提前谢谢

代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include <ctime>
#include <cstdlib>

using namespace std;

void merge(int[],int,int[],int,int[]);
void bubblemethod(int*,int);
void insertionmethod(int[],int);
void merge_sort(int[],int,int);
void mergemethod(int[],int,int,int);
void selectionmethod(int[],int,int);
int populateArray();

   int size;
   int*array;




int main(int argc, char** argv) {
    int menuOption;





    //int c2, n2;

 // printf("Ten random numbers in [1,100]\n");

  //for (c2 = 1; c2 <= 10; c2++) 
  //{
 //   n2 = rand()%100 + 1;
  //  printf("%d\n", n2);
  //}


    do

    {
    cout << endl << endl << "Select an option from the MENU:" << endl  ;
    cout << "1. Bubble Sort" << endl ;
    cout << "2. Insertion Sort" << endl ;
    cout << "3. Merge Sort" << endl  ;
    cout << "4. Selection Sort" << endl ;
    cout << "5. Quick Sort" << endl ;
    cout << "6. Exit the Program" << endl ;
    cin >> menuOption ;

        switch (menuOption)
        {
            case 1:

                size=populateArray();

                bubblemethod(array,size);



             break;

            case 2:

             size=populateArray();

                insertionmethod(array,size);


            break;
            /*
            case 3:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin >> size;

            randomnum=size;

             cout<< endl <<"The random numbers in this size are:" << endl;

               for (c1 = 1; c1 <= size; c1++) 
                  {
                      n1 = rand()%size + 1;
                      printf("%d\n", n1);
                      array[c1-1]=n1;
                  }

                  merge_sort(array,c1,randomnum);

                   cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
                   cout<<endl<<endl;

                   for(c1=1;c1<=randomnum;c1++)
                    cout<<array[c1-1]<<"    ";



            break;
            case 4:
                cout <<  endl << "Enter the size of the list with integers:" << endl ;
                cin>> size;

                randomnum=size;

                cout<< endl <<"The random numbers in this size are:" << endl;

               for (c1 = 1; c1 <= randomnum; c1++) 
                  {
                      n1 = rand()%size + 1;
                      printf("%d\n", n1);
                      array[c1-1]=n1;
                  }


                selectionmethod(array,c1,randomnum);

                break;
                case 5:

                break;
                case 6:
                cout << endl << "End the program" << endl  ;
                break;
                    */
            default:
                cout << endl << "Error in menu input. Valid menu options are 1 to 6." << endl  ;
        }// end switch


    }   while (menuOption !=6); // end do-while



}


void bubblemethod(int array[],int size)
            {
                int d,swap,c1;

            for (c1 = 0 ; c1 <size; c1++)
             {

                 for (d = 0 ; d < size - 1; d++)
                 {
                     if (array[d] > array[d+1]) /* For decreasing order use < */
                      {
                         swap   = array[d];
                         array[d]   = array[d+1];
                         array[d+1] = swap;
                      }
                 }
             }

            cout<< endl << "Sorted list:\n" << endl;


            for ( c1 = 0 ; c1 <size; c1++ )
            printf("%d\n", array[c1]);



            }

void insertionmethod(int array[],int size)
            {
                int d,t,c1;
                 for (c1 = 1 ; c1 <= size - 1; c1++) 
                 {
                   d = c1;

                   while ( d > 0 && array[d] < array[d-1]) 
                   {
                     t = array[d];
                     array[d]  = array[d-1];
                     array[d-1] = t;

                     d--;
                   }
                 }

                printf("Sorted list:\n");

                for (c1 = 0; c1 <= size - 1; c1++) 
                    printf("%d\n", array[c1]);

            }

            void merge_sort(int array[],int c1,int size)
              {
                  int mid;
                  if(c1<size)
                  {
                      mid=(c1+size)/2;
                      merge_sort(array,c1,mid);
                      merge_sort(array,mid+1,size);
                      mergemethod(array,c1,mid,size);
                  }
              } 

            void mergemethod(int array[],int c1,int mid,int randomnum)
            {
                int h,i,j,b[50],k;
                h=c1;
                i=c1;
                j=mid+1;

                while((h<=mid)&&(j<=randomnum))
                {
                   if(array[h]<=array[j])
                   {
                      b[i]=array[h];
                      h++;
                   }
                   else
                   {
                      b[i]=array[j];
                      j++;
                   }
                   i++;
                }
                if(h>mid)
                {
                   for(k=j;k<=randomnum;k++)
                   {
                      b[i]=array[k];
                      i++;
                   }
                }
                else
                {
                   for(k=h;k<=mid;k++)
                   {
                      b[i]=array[k];
                      i++;
                   }
                }
                for(k=c1;k<=randomnum;k++) 
                     array[k]=b[k];
            }



            void selectionmethod(int array[],int c1,int randomnum)
            {
                int position,d,swap;
                for ( c1 = 0 ; c1 < ( randomnum - 1 ) ; c1++ )
                {
                    position = c1;

                    for ( d = c1 + 1 ; d < randomnum ; d++ )
                    {
                         if ( array[position] > array[d] )
                         position = d;
                    }
                   if ( position != c1 )
                   {
                        swap = array[c1];
                        array[c1] = array[position];
                        array[position] = swap;
                   }
                }

                printf("Sorted list:\n");

                for ( c1 = 0 ; c1 < randomnum ; c1++ )
                printf("%d\n", array[c1]);
            }


int populateArray()
{
    int size;
cout <<  endl << "Enter the size of the list with integers:" << endl ;
                cin >> size;


                  int randomnum, c1, n1;

                 randomnum=size;
                 array=new int[size];

                  cout<< endl <<"The random numbers in this size are:" << endl;

               for (c1 = 1; c1 <= size; c1++) 
                  {
                      n1 = rand()%size + 1;
                      printf("%d\n", n1);
                      array[c1-1]=n1;
                  }

return size;    

}

这应该会有所帮助。

编辑您的代码,以秒为单位计算前两个案例的时间:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include <ctime>
#include <cstdlib>

using namespace std;

void merge(int[],int,int[],int,int[]);
void bubblemethod(int*,int);
void insertionmethod(int[],int);
void merge_sort(int[],int,int);
void mergemethod(int[],int,int,int);
void selectionmethod(int[],int,int);
int populateArray();

int size;
int*array;




int main(int argc, char** argv) {
    int menuOption;





    //int c2, n2;

    // printf("Ten random numbers in [1,100]\n");

    //for (c2 = 1; c2 <= 10; c2++) 
    //{
    //   n2 = rand()%100 + 1;
    //  printf("%d\n", n2);
    //}


    do

    {
        cout << endl << endl << "Select an option from the MENU:" << endl  ;
        cout << "1. Bubble Sort" << endl ;
        cout << "2. Insertion Sort" << endl ;
        cout << "3. Merge Sort" << endl  ;
        cout << "4. Selection Sort" << endl ;
        cout << "5. Quick Sort" << endl ;
        cout << "6. Exit the Program" << endl ;
        cin >> menuOption ;

        time_t t1 = 0;
        time_t t2 = 0;
        switch (menuOption)
        {
        case 1:

            size=populateArray();
            t1 = time(0);
            bubblemethod(array,size);
            t2 = time(0);

            break;

        case 2:

            size=populateArray();
             t1 = time(0);
            insertionmethod(array,size);
            t2 = time(0);


            break;
            /*
            case 3:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin >> size;

            randomnum=size;

            cout<< endl <<"The random numbers in this size are:" << endl;

            for (c1 = 1; c1 <= size; c1++) 
            {
            n1 = rand()%size + 1;
            printf("%d\n", n1);
            array[c1-1]=n1;
            }

            merge_sort(array,c1,randomnum);

            cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
            cout<<endl<<endl;

            for(c1=1;c1<=randomnum;c1++)
            cout<<array[c1-1]<<"    ";



            break;
            case 4:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin>> size;

            randomnum=size;

            cout<< endl <<"The random numbers in this size are:" << endl;

            for (c1 = 1; c1 <= randomnum; c1++) 
            {
            n1 = rand()%size + 1;
            printf("%d\n", n1);
            array[c1-1]=n1;
            }


            selectionmethod(array,c1,randomnum);

            break;
            case 5:

            break;
            case 6:
            cout << endl << "End the program" << endl  ;
            break;
            */
        default:
            cout << endl << "Error in menu input. Valid menu options are 1 to 6." << endl  ;
        }// end switch

        cout <<"time taken = "<<t2-t1<<" seconds";
    }   while (menuOption !=6); // end do-while



}


void bubblemethod(int array[],int size)
{
    int d,swap,c1;

    for (c1 = 0 ; c1 <size; c1++)
    {

        for (d = 0 ; d < size - 1; d++)
        {
            if (array[d] > array[d+1]) /* For decreasing order use < */
            {
                swap   = array[d];
                array[d]   = array[d+1];
                array[d+1] = swap;
            }
        }
    }

    cout<< endl << "Sorted list:\n" << endl;


    for ( c1 = 0 ; c1 <size; c1++ )
        printf("%d\n", array[c1]);



}

void insertionmethod(int array[],int size)
{
    int d,t,c1;
    for (c1 = 1 ; c1 <= size - 1; c1++) 
    {
        d = c1;

        while ( d > 0 && array[d] < array[d-1]) 
        {
            t = array[d];
            array[d]  = array[d-1];
            array[d-1] = t;

            d--;
        }
    }

    printf("Sorted list:\n");

    for (c1 = 0; c1 <= size - 1; c1++) 
        printf("%d\n", array[c1]);

}

void merge_sort(int array[],int c1,int size)
{
    int mid;
    if(c1<size)
    {
        mid=(c1+size)/2;
        merge_sort(array,c1,mid);
        merge_sort(array,mid+1,size);
        mergemethod(array,c1,mid,size);
    }
} 

void mergemethod(int array[],int c1,int mid,int randomnum)
{
    int h,i,j,b[50],k;
    h=c1;
    i=c1;
    j=mid+1;

    while((h<=mid)&&(j<=randomnum))
    {
        if(array[h]<=array[j])
        {
            b[i]=array[h];
            h++;
        }
        else
        {
            b[i]=array[j];
            j++;
        }
        i++;
    }
    if(h>mid)
    {
        for(k=j;k<=randomnum;k++)
        {
            b[i]=array[k];
            i++;
        }
    }
    else
    {
        for(k=h;k<=mid;k++)
        {
            b[i]=array[k];
            i++;
        }
    }
    for(k=c1;k<=randomnum;k++) 
        array[k]=b[k];
}



void selectionmethod(int array[],int c1,int randomnum)
{
    int position,d,swap;
    for ( c1 = 0 ; c1 < ( randomnum - 1 ) ; c1++ )
    {
        position = c1;

        for ( d = c1 + 1 ; d < randomnum ; d++ )
        {
            if ( array[position] > array[d] )
                position = d;
        }
        if ( position != c1 )
        {
            swap = array[c1];
            array[c1] = array[position];
            array[position] = swap;
        }
    }

    printf("Sorted list:\n");

    for ( c1 = 0 ; c1 < randomnum ; c1++ )
        printf("%d\n", array[c1]);
}


int populateArray()
{
    int size;
    cout <<  endl << "Enter the size of the list with integers:" << endl ;
    cin >> size;


    int randomnum, c1, n1;

    randomnum=size;
    array=new int[size];

    cout<< endl <<"The random numbers in this size are:" << endl;

    for (c1 = 1; c1 <= size; c1++) 
    {
        n1 = rand()%size + 1;
        printf("%d\n", n1);
        array[c1-1]=n1;
    }

    return size;    

}

使用IoStices库来编写一个格式化字符串,将时间表示为标准输出:STD::CUT抱歉,但对于C++来说是非常新的,我不知道如何编写这可能对您有帮助,但是如果您对C++非常陌生,它可能会有点混乱。从问题中更改代码,就像接受的答案所说的那样,然后您可以使用getresult\u of\u timer\u函数来获取时间。关于令人困惑的部分,您是对的,我还没有在我的程序中编写任何计时器,所以不确定我应该更改什么?你想让我把我的代码复制到这里,这样你就能知道了吗?再次感谢你试图帮助我!是的,它确实有帮助,我在我的主代码中复制了这个代码?因为我已经有了这个int main argc,char**argv{看起来如果我不这样做,它会导致冲突,//code go here注释中的内容?谢谢!你可以将你的代码放在这里,你想计算它的时间。我无法发布代码我试图编辑我的原始帖子,但其中一半没有作为代码发布,它不允许我发布。你可以将timer.h头文件添加到你的库中。然后创建变量:double start,end;call,start=get_time;/…您的代码../end=get_time;printfTime take,end-start;done!:再次感谢您,祝您度过愉快的一天!如果有什么我能做的,请随时给我发一封邮件!
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string>
#include <ctime>
#include <cstdlib>

using namespace std;

void merge(int[],int,int[],int,int[]);
void bubblemethod(int*,int);
void insertionmethod(int[],int);
void merge_sort(int[],int,int);
void mergemethod(int[],int,int,int);
void selectionmethod(int[],int,int);
int populateArray();

int size;
int*array;




int main(int argc, char** argv) {
    int menuOption;





    //int c2, n2;

    // printf("Ten random numbers in [1,100]\n");

    //for (c2 = 1; c2 <= 10; c2++) 
    //{
    //   n2 = rand()%100 + 1;
    //  printf("%d\n", n2);
    //}


    do

    {
        cout << endl << endl << "Select an option from the MENU:" << endl  ;
        cout << "1. Bubble Sort" << endl ;
        cout << "2. Insertion Sort" << endl ;
        cout << "3. Merge Sort" << endl  ;
        cout << "4. Selection Sort" << endl ;
        cout << "5. Quick Sort" << endl ;
        cout << "6. Exit the Program" << endl ;
        cin >> menuOption ;

        time_t t1 = 0;
        time_t t2 = 0;
        switch (menuOption)
        {
        case 1:

            size=populateArray();
            t1 = time(0);
            bubblemethod(array,size);
            t2 = time(0);

            break;

        case 2:

            size=populateArray();
             t1 = time(0);
            insertionmethod(array,size);
            t2 = time(0);


            break;
            /*
            case 3:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin >> size;

            randomnum=size;

            cout<< endl <<"The random numbers in this size are:" << endl;

            for (c1 = 1; c1 <= size; c1++) 
            {
            n1 = rand()%size + 1;
            printf("%d\n", n1);
            array[c1-1]=n1;
            }

            merge_sort(array,c1,randomnum);

            cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;
            cout<<endl<<endl;

            for(c1=1;c1<=randomnum;c1++)
            cout<<array[c1-1]<<"    ";



            break;
            case 4:
            cout <<  endl << "Enter the size of the list with integers:" << endl ;
            cin>> size;

            randomnum=size;

            cout<< endl <<"The random numbers in this size are:" << endl;

            for (c1 = 1; c1 <= randomnum; c1++) 
            {
            n1 = rand()%size + 1;
            printf("%d\n", n1);
            array[c1-1]=n1;
            }


            selectionmethod(array,c1,randomnum);

            break;
            case 5:

            break;
            case 6:
            cout << endl << "End the program" << endl  ;
            break;
            */
        default:
            cout << endl << "Error in menu input. Valid menu options are 1 to 6." << endl  ;
        }// end switch

        cout <<"time taken = "<<t2-t1<<" seconds";
    }   while (menuOption !=6); // end do-while



}


void bubblemethod(int array[],int size)
{
    int d,swap,c1;

    for (c1 = 0 ; c1 <size; c1++)
    {

        for (d = 0 ; d < size - 1; d++)
        {
            if (array[d] > array[d+1]) /* For decreasing order use < */
            {
                swap   = array[d];
                array[d]   = array[d+1];
                array[d+1] = swap;
            }
        }
    }

    cout<< endl << "Sorted list:\n" << endl;


    for ( c1 = 0 ; c1 <size; c1++ )
        printf("%d\n", array[c1]);



}

void insertionmethod(int array[],int size)
{
    int d,t,c1;
    for (c1 = 1 ; c1 <= size - 1; c1++) 
    {
        d = c1;

        while ( d > 0 && array[d] < array[d-1]) 
        {
            t = array[d];
            array[d]  = array[d-1];
            array[d-1] = t;

            d--;
        }
    }

    printf("Sorted list:\n");

    for (c1 = 0; c1 <= size - 1; c1++) 
        printf("%d\n", array[c1]);

}

void merge_sort(int array[],int c1,int size)
{
    int mid;
    if(c1<size)
    {
        mid=(c1+size)/2;
        merge_sort(array,c1,mid);
        merge_sort(array,mid+1,size);
        mergemethod(array,c1,mid,size);
    }
} 

void mergemethod(int array[],int c1,int mid,int randomnum)
{
    int h,i,j,b[50],k;
    h=c1;
    i=c1;
    j=mid+1;

    while((h<=mid)&&(j<=randomnum))
    {
        if(array[h]<=array[j])
        {
            b[i]=array[h];
            h++;
        }
        else
        {
            b[i]=array[j];
            j++;
        }
        i++;
    }
    if(h>mid)
    {
        for(k=j;k<=randomnum;k++)
        {
            b[i]=array[k];
            i++;
        }
    }
    else
    {
        for(k=h;k<=mid;k++)
        {
            b[i]=array[k];
            i++;
        }
    }
    for(k=c1;k<=randomnum;k++) 
        array[k]=b[k];
}



void selectionmethod(int array[],int c1,int randomnum)
{
    int position,d,swap;
    for ( c1 = 0 ; c1 < ( randomnum - 1 ) ; c1++ )
    {
        position = c1;

        for ( d = c1 + 1 ; d < randomnum ; d++ )
        {
            if ( array[position] > array[d] )
                position = d;
        }
        if ( position != c1 )
        {
            swap = array[c1];
            array[c1] = array[position];
            array[position] = swap;
        }
    }

    printf("Sorted list:\n");

    for ( c1 = 0 ; c1 < randomnum ; c1++ )
        printf("%d\n", array[c1]);
}


int populateArray()
{
    int size;
    cout <<  endl << "Enter the size of the list with integers:" << endl ;
    cin >> size;


    int randomnum, c1, n1;

    randomnum=size;
    array=new int[size];

    cout<< endl <<"The random numbers in this size are:" << endl;

    for (c1 = 1; c1 <= size; c1++) 
    {
        n1 = rand()%size + 1;
        printf("%d\n", n1);
        array[c1-1]=n1;
    }

    return size;    

}