如何使这个C#代码减少处理器密集? [英] How can I make this C# code less processor-intensive?

查看:76
本文介绍了如何使这个C#代码减少处理器密集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的代码转换为更简单的版本。当前看起来太复杂,和/或太慢,无法创建数百个命令。请解释其他方式。我已经尝试将它放入switch语句中,但它不起作用,因为它是一个方法,而不是变量。我试过的例子:



I would like to turn my code into a simpler version. The current seems too complex, and/or too slow to create hundreds of commands. Please explain other ways. I already attempted to put it into a switch statement, but it doesn't work because it's a method, not variable. Example of what I tried:

switch (rawCmd.Contains("Help")) {

}



(很明显我会将案件放在那里。)



我当前的代码在那里v



提前多多感谢!!



我尝试过的方法:




(And obviously I would have put the cases in there.)

My current code is down there v

Thanks a lot in advance!!

What I have tried:

public static String ParseCmd(String rawCmd)
        {
        if (rawCmd.Contains("help") || rawCmd.Contains("Help") || rawCmd.Contains("HELP"))

        {
            return "Type a command!";
        }
        return null;
    }

推荐答案

首先,你的switch语句不起作用,因为String.Contains()返回一个bool。

First of all, your switch statement doesn't work because String.Contains() returns a bool.
switch (rawCmd.Contains("Help"))
{
 
}



应该是


should be

switch (rawCmd)
{
    case : "help" : ..; break;
    case : "heLP" : ..; break;
    case : "HeLP" : ..; break;
    etc. 
    default: // maybe throw an exception
}



但是如你所见,你会遇到很多情况,这只是针对一个命令。

最好像评论中建议的那样做,并使用.ToLower或.ToUpper。


But as you see you will have a lot of cases, and this just for one command.
So better to do like suggested in the comment and use either .ToLower or .ToUpper.

if (rawCmd.ToUpper().Equals("HELP"))
{
    // Do something
}



或者如果你有很多命令


or if you have many commands

switch (rawCmd.ToUpper())
{
    case "HELP" : ...; break;
    etc. 
    default: // maybe throw an exception
}


因为你将拥有数百甚至数千条命令,所以我会建议使用字典< string,string> 。类似于:

Since you'll have "hundreds, maybe thousands of commands" then I'd suggest using a Dictionary<string, string>. Something like:
private static Dictionary<string, string> CommandMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) {
    { "Help", "Type a command!" },
    { "Quit", "Goodbye!" },
    { "MyCommand", "My Command response" },
    // etc... { commandstring, responsestring }
  };
public static string ParseCmd(string rawCmd)
{
  string response;
  if (!CommandMap.TryGetValue(rawCmd, out response)
  {
    // here's where you could respond to an unrecognized command
    // or you could just let ParseCmd return null and handle it somewhere else.
  }
  return response;
}





但你暗示你有子命令,所以你可能需要有一个不同的结构,每个顶级命令返回解析特定子命令所需的信息。这些信息取决于你正在做什么。

我怀疑一些一种树状结构。



But you implied that you had subcommands so you might need to have a different structure, where each top-level command returns the information necessary to parse the specific subcommand. What that information is depends on what you're doing.
I suspect some sort of tree structure.


如果你开始检查病例,你需要检查所有组合。

help,helP,heLp,heLP,hElp,hElP ,hELp,hELP,帮助,HelP,HeLp,他LP,HElp,HElP,HELP和HELP。

最好的解决方案是强制小写或大写,你的选择。

If you start to check for cases, you need to check all combinations.
help, helP, heLp, heLP, hElp, hElP, hELp, hELP, Help, HelP, HeLp, HeLP, HElp, HElP, HELp and HELP.
The best solution is to force to lower case or upper case, your choice.
public static String ParseCmd(String rawCmd)
        {
        string TmpCmd= rawCmd.ToUpper();
        if (TmpCmd.Equals("HELP")
        {
            return "help!";
        }
        else if (TmpCmd.Equals("QUIT")
        {
            return "Quit!";
        }
        else if (TmpCmd.Equals("MYCOMMAND")
        {
            return "My Command!";
        }
        else
        {
            return "Unknown Command!";
        }
        return null;
    }





[更新]

如果切换执行相同的操作。区别在于如果允许更复杂的条件。

对于数百或数千个命令,将使用其他技术来防止必须检查每个可能性。

需要更多细节来确定哪种技术最好。



[Update]
if and switch perform the same. The difference is that if allow more sophisticated conditions.
For hundred or thousands of commands, other techniques are to be used to prevent having to check every possibilities.
More details would be needed to determinate which technique is best.


这篇关于如何使这个C#代码减少处理器密集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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