如何在C#中更快地创建此代码 [英] How can I make this code faster in C#

查看:79
本文介绍了如何在C#中更快地创建此代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/*class Car:IVehicle //the class, just to see what's needed (license plate number which is always like AAA111 - BBB222 so 3 letter and 3 number and the color) 
    {
        string lpn;
        public string LPN { get { return lpn; } }
        string color;
        public string Color { get { return color; } }
        public Car(string lpn, string color)
        {
            this.lpn = lpn;
            this.color = color;
        }
    }

List<ivehicle> vehicles=new List<ivehicle>(); //I'm using a list so that's why i'm showing that to you
List<int> penalities=new List<int>();*/

public bool Suspicious(IVehicle veh) //this method job would be if the police can't see the license plate number perfectly (first you should check if the colors are the same as you can see in the code) and with this you can check if there is a little mistake in the lpn example (in list AAA111 & red, and you run the method x.Suspicios(new Car("AAX11X","red")) it gives you back true if atleast 4 char is same in your list, so e.g. x.Suspicious(new Car("AXX11X","red")) gives back false, or if i change the color so it does give back false
        {
            foreach (Car x in vehicles)
            {
                if (x.Color == veh.Color)
                {
                    int coun = 0;
                    int i = 0;
                    while (coun < 4 && i < 6)
                    {
                        if (x.LPN[i] == veh.LPN[i])
                            db++;
                        i++;
                        if (db > 3)
                            return true;
                    }
                }
            }
            return false;
        }

//in the main
log x=new log();
x.Initiate(3);
x.NewIrregular(new Car("AAA111","red"),1000);
bool sus=x.Suspicious(new Car("AAX11X","red");//so atm it gives back true





我尝试了什么:



i尝试使用数组练习它比较慢,我之前有4-5个代码,这是最简单,最快的,资源量就像没关系,它应该很快...

我正在尝试做它与lambda但我远非专家,我无法弄清楚我怎么能用lambda写它...

有人能帮助我吗?



What I have tried:

i tried this exercise with arrays but it was slower and i had like 4-5 codes before that, and that was the simplest and fastest, the resource amount is like nevermind, it should be fast...
I'm trying to do it with lambda but i'm far from expert and i can't figure it out how can i write it in lambda...
Could anyone help me, please?

推荐答案

你正在对未知大小的列表(车辆)进行运行,所以即使是一次检查也很快,你无法预测最后的时间(它实际上是O(颜色)+ O(n) -colo r))...

由于您对任何车辆的颜色与您传递的颜色不同,您可以预先选择相同颜色的车...这样的事情:

You are running against a list of unknown size (vehicles), so even a single check is quick, you can not predict the final time (it is actually O(color) + O(n-color))...
As you are not interested in any car has different color from the one you passed in, you may do a pre-select of only the cars with the same color... Something like this:
var CheckList = vehicles.select(car => car.Color == veh.Color);
foreach (Car x in CheckList)
{
  // ...
}



你的内在计算也不清楚......

coun 接缝未被使用和不必要......

你也可以削减支票,即使所有字母都是马赫,你也不会到达4边界...(类似于(db +(6 - i))< 4)


Your inner computation is also unclear...
coun seams to be unused and unnecessary...
You also can cut the check if even all the letters left are mach you will not reach the 4 boundary... (something like (db + (6 - i)) < 4)


只是为了让你开始分析。

分析(计算机编程) - 维基百科 [ ^ ]

< a href =http://resources.mpi-inf.mpg.de/departments/rg1/teaching/advancedc-ws08/script/lecture06.pdf>分析 [ ^ ]

软件工程/测试/分析简介 - 维基教科书,为开放世界打开书籍 [ ^ ]
Just to get you started on profiling.
Profiling (computer programming) - Wikipedia[^]
Profiling[^]
Introduction to Software Engineering/Testing/Profiling - Wikibooks, open books for an open world[^]


这篇关于如何在C#中更快地创建此代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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