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

时间:2015-03-20 10:58:17

标签: c++ big-o execution-time

我的代码是:

#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秒。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我认为您的问题与overlap功能有关。

在其中,你有

  • 排序调用:O(n×ln(n))
  • 两个for循环:
    • 第一个大概是0..Size
    • 第二个(嵌套在第一个)大致为i..Size

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

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