如何在C#中优化我的zip函数以使其更快?

时间:2019-06-17 21:17:54

标签: c# .net mongodb foreach webdriver

如何优化代码的“ findelements”和“ tolist”部分以使其运行更快?我通过cssselectors制作了一个字段标题列表和它的值列表,将它们压缩,然后将它们扔到我的数据库中。这是我程序中最慢的部分,我想知道如何才能加快该过程的速度。请帮忙谢谢你<3

    public static void zipper()
    {
        //Database connection
        MongoClient dbClient = new MongoClient(mongoinfo);
        var db = dbClient.GetDatabase("TestDB");
        var collec = db.GetCollection<BsonDocument>("TestDB");

        //Zip editList
        foreach (var item in editList)
        {

            //Console.WriteLine(newURL);
            driver.Navigate().GoToUrl(item);

            IList<String> keys = new List<string>();
            IList<IWebElement> keyList = 
            driver.FindElements(By.CssSelector("td.Field.box"))
            .ToList();
            foreach (IWebElement key in keyList)
            {
                if (key.Text != " Person authorizing adjustment")
                {
                    keys.Add(key.Text.TrimStart(' '));
                }
            }
            IList<String> vals = new List<string>();
            IList<IWebElement> valList = 
            driver.FindElements(By.CssSelector("td.Data.box"))
            .ToList();
            foreach (IWebElement val in valList)
            {
                vals.Add(val.Text);
            }

            int keynum = keys.Count();
            Console.WriteLine(keynum);
            int valnum = vals.Count();
            Console.WriteLine(valnum);
            var keysAndVals = keys.Zip(vals, (first,second) => 
            first+":"+second);
            IList<String> kv = keysAndVals.ToList();
            BsonArray bArray = new BsonArray();
            foreach (var term in kv)
            {
                Console.WriteLine(term);
                bArray.Add(term);
            }
            var document = new BsonDocument();

            document.Add("TestDB", bArray);
            collec.InsertOneAsync(document);
        }
    }

我可以使用隐式等待吗? 还是我缺少需要在某个地方使用的异步/等待?

0 个答案:

没有答案