计算我的c++程序的执行时间和大O时间

Calculating Execution time and big O time of my c++ program

本文关键字:时间 执行时间 我的 c++ 程序 计算      更新时间:2023-10-16

我的代码是:

#include <iostream>
#include <utility>
#include <algorithm>
//#include <iomanip>
#include <cstdio>
//using namespace std;
inline int overlap(std::pair<int,int> classes[],int size)
{
  std::sort(classes,classes+size);
  int count=0,count1=0,count2=0;
  int tempi,tempk=1;
  for(unsigned int i=0;i<(size-1);++i)
  {
      tempi = classes[i].second;
      for(register unsigned int j=i+1;j<size;++j)
      {
          if(!(classes[i].first<classes[j].second && classes[i].second>classes[j].first))
          {   if(count1 ==1)
              {
                  count2++;
              }
              if(classes[i].second == tempi)
              {
                  tempk =j;
                  count1 = 1;
              }
              ////cout<<"n"<<"Non-Overlapping Class:t";
              ////cout<<classes[i].first<<"t"<<classes[i].second<<"t"<<classes[j].first<<"t"<<classes[j].second<<"n";
              classes[i].second = classes[j].second;
              count++;
              if(count1==1 && j ==(size-1))
              {
                 j= tempk;
                 classes[i].second = tempi;
                 count1= 0;
                 if(count2 !=0)
                 {
                    count = (count + ((count2)-1));
                 }
                 count2 =0;
              }
          } 
          else
          {
              if(j ==(size-1))
              {
                 if(count>0)
                 {
                 j= tempk;
                 classes[i].second = tempi;
                 count1= 0;
                 if(count2 !=0)
                 {
                  count = (count + ((count2)-1));
                 }
                 count2 =0;
                 }
              }
          }
      }
  }
  count = count + size;
  return count;
}
inline int fastRead_int(int &x) {
    register int c = getchar_unlocked();
    x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar_unlocked());
    if(c=='-') {
        neg = 1;
        c = getchar_unlocked();
    }
    for(; c>47 && c<58 ; c = getchar_unlocked()) {
        x = (x<<1) + (x<<3) + c - 48;
    }
    if(neg)
        x = -x;
 return x;
}
int main()
{
   int N;
   ////cout<<"Please Enter Number Of Classes:";
   clock_t begin,end;
   float time_interval;
   begin = clock();
   while(fastRead_int(N))
   {
   switch(N)
   {
    case -1 : end = clock();
              time_interval = float(end - begin)/CLOCKS_PER_SEC;
              printf("Execution Time = %f",time_interval);
              return 0;
    default : 
     unsigned int subsets;
     unsigned int  No = N;
     std::pair<int,int> classes[N];
     while(No--)
     {
       ////cout<<"Please Enter Class"<<(i+1)<<"Start Time and End Time:";
       int S, E;
       fastRead_int(S);
       fastRead_int(E);
       classes[N-(No+1)] = std::make_pair(S,E);
     }
     subsets = overlap(classes,N);
     ////cout<<"n"<<"Total Number Of Non-Overlapping Classes is:";
     printf("%08d",subsets);
     printf("n");
     break;
   }
   }
}

以及我的程序的输入和输出:

Input:
5
1 3
3 5
5 7
2 4
4 6
3
500000000 1000000000
1 5
1 5
1 
999999999 1000000000
-1
Output:
Success time: 0 memory: 3148 signal:0
00000012
00000005
00000001
Execution Time = 0.000036

我试着把钟放在主旋律的开头和结尾来计算,并找出了时间。但它只显示了0.000036秒。但当我试图在在线法官(SPOJ)中发布相同的代码时。我的程序出现"超过时间限制"错误。SPOJ中上述程序的时间限制为2.365秒。有人能帮我弄清楚吗?

我认为您的问题是关于overlap函数的。

在里面,你有

  • 一个排序调用:O(n×ln(n))
  • 两个CCD_ 2环路:
    • 第一个大致为0..Size
    • 第二个(嵌套在第一个中)大致为i..Size

第二个循环内部的被称为Size(Size+1)/2(N个第一个整数的逆和)次,没有中断。

所以你的算法是O(n²),其中n是大小。