【问题标题】:Calculating Execution time and big O time of my c++ program计算我的 C++ 程序的执行时间和大 O 时间
【发布时间】:2015-03-20 10:58: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

我试图通过在 main 开始和 main 结束时设置时钟来计算时间。但它说只有大约 0.000036 秒。但是当我尝试在 Online Judge(SPOJ) 中发布相同的代码时。我的程序出现“超出时间限制”错误。 SPOJ 中上述程序的时间限制是 2.365 秒。有人可以帮我解决这个问题吗?

【问题讨论】:

  • 你不能用时间戳计算 big(O)。这些严格取决于一个人的设置。如果你有一个平均需要 30 秒的算法,当你在超级计算机上运行它时,你会立即得到结果。这准确吗?
  • 可能是SPOJ的隐藏测试用例太大..你应该优化你的代码。
  • Big O 和性能是两个不同的东西。 Big O 是算法的分类,具体取决于您执行的重要操作的数量。性能是您在特定配置上规划票价的方式。
  • “有人可以帮我解决这个问题吗?” 这很模糊。您是否尝试过请朋友或导师指导您完成它?我看不出这个问题将来会有什么用。
  • 如果您能告诉我们您要解决的问题,那就太好了!

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


【解决方案1】:

我认为您的问题与overlap 函数有关。

你有

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

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

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

【讨论】:

  • 嗨 Orace...感谢您的回答...我知道 O(n^2) 是时间复杂度中最坏的情况。有没有办法在不影响功能的情况下实现相同代码的 O(nlogn)(或更好的性能)?
  • 我尽力优化了上面的代码。使用 getchar_unlimited() 而不是 cin。并将寄存器变量用于内部循环。使用 While 循环来获取输入而不是 for 循环。我还能做些什么来提高代码的性能?
  • 那些是本地优化,en.wikipedia.org/wiki/Program_optimization。正如我所说,编译器可能会比你做得更好。您只需将时间除以一个常数。 O(n²/k) 是 O(n²)。这不是你要找的。您必须更改算法设计以达到 O(n×ln(n)) (如果可能的话)。另外,如果您需要任何帮助,请解释您的算法的作用。
  • SPOJ ACTIV - spoj.com/problems/ACTIV 是我试图解决的问题。我对时间间隔进行了排序,然后尝试将第一个时间间隔与下一个时间间隔进行比较,以检查它是否重叠。如果它们不重叠,我将这两个时间间隔合并并与下一个时间间隔检查。同样,直到最后一个时间间隔正在进行。一旦到达最后一个,我从第二个间隔到最后一个,然后从第三个间隔到最后一个继续相同的过程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多