简体   繁体   中英

Reduce the execution time

I have an excel file as well as a config xml file.This config xml file contain some keys that are present in the excel file.My goal is to parse the xml file and findout whether the keys are present in excel file.I used 2 loops for doing this.It is taking a lot of time to complete.My syntax of code is given below.

foreach(node in xmlfile)
{
    foreach(key in excelfile)
    {
        if(key.Equals(node))
        {
            print node +"found"
        }
    }
}

The issue of the above code is that it is taking a lot of time to search through the excel file. My excel file is so large and there are lot of keys other than that in the xml file.

Is there any other better way to do this task?

The solution is about as inefficient as possible. That's because

foreach(node in xmlfile)
{
    foreach(key in excelfile)
    {
        if(key.Equals(node))
        {

Will perform xmlfile's rowcount * excelfile's rowcount comparisions. When you got, say two files á 1 000 rows, you'll do 1 000 * 1 000 = 1 000 000 comparision operations.

Instead of such an overwhelming work, take one of the files and read node values into a hashtable. Then loop once through the other file and check if the value is found from the hashtable.

This might not be your final solution, but it's very simple and effective, just make the bigger loop as an outer loop and the small loop is the inner one, then break upon finding your key:

foreach(key in excelfile)
{
    foreach(node in xmlfile)
    {
        if(key.Equals(node))
        {
            print node +"found"
            break;
        }
    }
}

this will eliminate the overhead looping over the whole big excel file for every xml iteration (when using break, otherwise there is no differnce), instead, it will loop over the small xml file for each excel iteration, and even break upon matching.

One option is to build an alternating regex from one of the collections, and then use that with the -match operator against the other collection:

$collection = 1..10
$even = 2,4,6,8,10

[regex]$even_regex = ‘(?i)^(‘ + (($even |foreach {[regex]::escape($_)}) –join “|”) + ‘)$’

$collection -match $even_regex |
  foreach { "Found $_ "}

Found 2 
Found 4 
Found 6 
Found 8 
Found 10 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM