优化此C代码(150000输入) [英] Optimize this C code (150000 inputs)

查看:90
本文介绍了优化此C代码(150000输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码需要26秒钟的时间才能执行.请优化,但必须输入150000个输入.

This code is taking 26 seconds for execution. Please optimize, but it must take 150000 inputs.

#include <stdlib.h>
#include<stdio.h>
#include<time.h>

int q1, q2, q3, q4;
 void quadrant(long temp1,long temp2)
 {

        if(temp1&gt;=0&&temp2&gt;=0)
       q1++;
     else if(temp1&gt;=0&&temp2&lt;=0)
       q4++;
     else if(temp1&lt;0&&temp2&lt;0)
       q3++;
     else if(temp1&lt;=0&&temp2&gt;=0)
        q2++;

  }

 int main()
 {
time_t start,end;
  volatile long unsigned t;

  start = time(NULL);
  
  

int a[100000][2],c,d,n,i,j,will,b[102420][4],k=0;
 char p;
 //printf("enter the number of co ordinate");
 scanf("%d",&n);
 for(i=0;i&lt;n;i++)&gt;
 for(j=0;j&lt;2;j++)
 scanf("%d",&a[i][j]);
 //printf("enter no of operations");
scanf("%d",&will);
while(will&gt;0)
{
p=getchar();
scanf("%c",&p);
switch(p)
{
case 'X':               scanf("%d %d",&c,&d);
                 for(i=c-1;i&lt;d;i++)&gt;
                 a[i][1]=-(a[i][1]);break;
case 'Y':
               scanf("%d %d",&c,&d);
               for(i=c-1;i&lt;d;i++)&gt;
               a[i][0]=-(a[i][0]);break;
case 'C':       scanf("%d %d",&c,&d);
               for(i=c-1;i&lt;=d-1;i++)
               {
                int temp1,temp2;
                temp1=a[i][0];
                temp2=a[i][1];

                quadrant(temp1,temp2);
               }
              b[k][0]=q1;
              b[k][1]=q2 ;
              b[k][2]=q3;
              b[k][3]=q4;
                 k=k+1;
   q1=0;
   q2=0;
   q3=0;
   q4=0;
   break;
case 'p':   printf(" the co-ordinates are:\n");
              for(i=0;i&lt;n;i++)&gt;
              {
                for(j=0;j&lt;2;j++)
               {
             printf("%d   ",a[i][j]);
                }
               printf("\n");
                }
                break;
default: printf("enter the correct choice\n");break;
}
if(will==1)
for(i=0;i&lt;k;i++)&gt;
{
printf("%d %d %d %d\n",b[i][0],b[i][1],b[i][2],b[i][3]);

}
will--;
}
 end = time(NULL);
  
  printf("Loop used %f seconds.\n", difftime(end, start));

}

推荐答案

如果您希望人们为您做工作,那么您必须为他们做一些事情:
1)付钱给他们.这使其他选项变得不必要,但仍然是明智的.
2)要有礼貌. 问"他们这样做,而不仅仅是告诉他们.
3)解释为什么你做不到.
4)使代码易于阅读和理解:
4a)评论.
4b)缩进和格式化
4c)使用明智的变量名称.
4d)要保持一致.

由于您没有做任何事情,因此您可以走开(做些小动作),然后自己做.
If you want people to do jobs for you, then you have to do something for them:
1) Pay them. This renders the other options unnecessary, but still advisable.
2) Be polite. "Ask" them to do it, not just tell them.
3) Explain why you can''t do it.
4) Make the code easy to read and understand:
4a) Comments.
4b) Indentation and and formatting
4c) Use sensible variable names.
4d) Be consistent.

Since you have done none of these things, you can go away (in small jerky movements), and do it yourself.


您要在与用户互动的循环中进行优化(实际上,您正在测量这种循环的性能).这对我来说毫无意义.
You are asking for optimization on a loop that has interaction with the user (and in fact you are measuring the performance of such a loop). It makes little sense to me.


这篇关于优化此C代码(150000输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆