这看起来很慢 - 如何做得更好? [英] This looks slow - how to do it better?

查看:76
本文介绍了这看起来很慢 - 如何做得更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从5个midi端口中的一个发送数据,如果m_iCurrentStrip + 1在1到8之间,则数据将从midi端口0输出,如果m_iCurrentStrip + 1在9到16之间,则数据将去输出端口1,依此类推最多5个端口。



无法以优雅的方式将其可视化我只是使用了一系列if if else'的<交换语句中的




它有效,但是很好。我不喜欢看它。



你会怎么做?





  int  port;  //   usb或midi port(SurfaceAddress [port]  
开关(NUM_UNITS)
{
案例 0 // 没有单位
port = 0 ;
break ;
case 1 // 有1个单位
port = 0 ;
break ;

case 2 // 有两个单位
如果(m_iCurrentStrip + 1> 8
{
port = 1 ;
} else {
port = 0 ;
}
break ;

case 3 / / 有三个单位
if (m_iCurrentStrip + 1> 16
{
port = 2 ;
} 其他 {
如果(m_iCurrentStrip + 1> 8
{
port = 1 ;
} else {
if (m_iCurrentStrip + 1< 8
{
port = 0 ;
}
}
}
case 4 // 有四个单位
如果(m_iCurrentStrip + 1> 24
{
port = 3 ;
} 其他 {
如果(m_iCurrentStrip + 1> 16
{
port = 2 ;
} 其他 {
如果(m_iCurrentStrip + 1> 8
{
port = 1 ;
} else {
if (m_iCurrentStrip + 1< 8
{
port = 0 ;
}
}
}
}
案例 5 // 有五个单位
if (m_iCurrentStrip + 1> 32
{
port = 4 ;
} 其他 {
如果(m_iCurrentStrip + 1> 24
{
port = 3 ;
} 其他 {
如果(m_iCurrentStrip + 1> 16
{
port = 2 ;
} 其他 {
如果(m_iCurrentStrip + 1> 8
{
port = 1 ;
} else {
if (m_iCurrentStrip + 1< 8
{
port = 0 ;
}
}
}
}
}
break ;
}

/ xml>

解决方案

如果m_iCurrentStrip + 1等于8,则会失败。除此之外,情况0和1可以通过。如果这是C ++我将使用一个地图或C#中的一个字典来让一行代码查找正确的值。你也可以打开所有的值(如案例16:案例15:依此类推),但在C中,这可能会很好。





实际上,在我看来,案件之间规则的唯一区别在于,有多少。我会把它放到一个if语句中,并添加如下的检查:



  if (m_iCurrentStrip + 1 >   32 
{
如果(NUM_UNITS > 4
port = 4 ;
} else {
if (m_iCurrentStrip + 1 > 24mp;& NUM_UNITS > 4
{
if (NUM_UNITS > 3
port = 3 ;
} 其他 {
如果(m_iCurrentStrip + 1& gt; 16
{
if (NUM_UNITS > 2
port = 2 ;
} 其他 {
如果(m_iCurrentStrip + 1& gt; 8
{
if (NUM_UNITS > 1
port = 1 ;
} else {

port = 0 ;
}


也许你可以用一行来做到这一点:

 port = NUM​​_UNITS ==  0  0 :min((m_iCurrentStrip + 1)/  8 ,NUM_UNITS  -   1 ); 


I have to send data out one of 5 midi ports where if the m_iCurrentStrip+1 is between 1 and 8 the data is to go out midi port 0, if the m_iCurrentStrip+1 is between 9 and 16 the data is to go out to port 1 and so on for up to 5 ports.

Unable to visualize this in a elegant way I simply used a series of if then else''s
inside a switch statement.

It works, but bleh. I hate looking at it.

How would you do it?


int port; // usb or midi port (SurfaceAddress[port]
switch(NUM_UNITS)
{
	case 0: // there are no units
	    port = 0;
	    break;
	case 1:    // there is 1 unit
	    port = 0;
	    break;

	case 2:// there are two units
										                   if(m_iCurrentStrip+1  > 8)
		{
	            port = 1;
                }else{
	            port = 0;
		}
                 break;

	case 3:// there are three units
										                    if(m_iCurrentStrip+1 > 16)
		{
	             port = 2;
                 }else{
											          if(m_iCurrentStrip+1 > 8)
	         {
		      port = 1;
	         }else{
												 if(m_iCurrentStrip+1 < 8)
		 {
			port = 0;
		 }
	      }
										}
	case 4:// there are four units
										                   if(m_iCurrentStrip+1 > 24)
		{
	               port = 3;
                }else{
										                   if(m_iCurrentStrip+1 > 16)
		{
	               port = 2;
	        }else{
											         if(m_iCurrentStrip+1 > 8)
	         {
	               port = 1;
	         }else{
												 if(m_iCurrentStrip+1 < 8)
		{
			port = 0;
		}
	      }
            }
          }
	case 5:// there are five units
										                    if(m_iCurrentStrip+1 > 32)
		{
	                port = 4;
                 }else{
										                    if(m_iCurrentStrip+1 > 24)
		 {
	                port = 3;
                   }else{
										                     if(m_iCurrentStrip+1 > 16)
		  {
	                port = 2;
                   }else{
											           if(m_iCurrentStrip+1 > 8)
	           {
	                port = 1;
											            }else{
												    if(m_iCurrentStrip+1 < 8)
		    {
			port = 0;
	            }
											           }
		  }
		 }
		}
												break;
}

/xml>

解决方案

This fails if m_iCurrentStrip + 1 equals 8. Apart from that, case 0 and 1 can fall through. If this was C++ I''d use a map, or a Dictionary in C#, to have one line of code that looks up the right value. You could also switch on all the values ( as in case 16: case 15: and so on ), but in C, this could be as good as it gets.


Actually, it looks to me like the only difference in the rules between cases, is how many there are. I''d drop this in to one if statement, and add checks such as:

if(m_iCurrentStrip+1 > 32)
       {
             if (NUM_UNITS > 4 )  
                   port = 4;
                }else{
                                                           if(m_iCurrentStrip+1 > 24mp;& NUM_UNITS > 4)
       {
             if (NUM_UNITS > 3 )  
                  port = 3;
                 }else{
                                                           if(m_iCurrentStrip+1 &gt; 16)
        {
             if (NUM_UNITS > 2 )  
                  port = 2;
                 }else{
                                                     if(m_iCurrentStrip+1 &gt; 8)
             {
             if (NUM_UNITS > 1 )  
                  port = 1;
                                                      }else{
                                                  
          port = 0;
              }


Probably you can do this using a single line:

port = NUM_UNITS == 0 ? 0 : min((m_iCurrentStrip+1) / 8, NUM_UNITS - 1);


这篇关于这看起来很慢 - 如何做得更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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