Before looping through an Excel document using a library I found, and wanted to know how long it would take to loop through the whole thing.
There are 40k rows.
I looped through only the first 5k and it took just 2 minutes. So, I thought, that looping through 40k would take just 16 minutes total.
However, the program has been running for over an hour now, and it still hasn't completed.
What would be slowing this thing down? Granted, I didn't program it for high efficiency, but there must be something that is slowing this down... Why wasn't my initial estimate accurate?
Here is my method:
public static void Validate(ExcelConfiguration config)
{
var ctx = new IntakeEntities();
Stopwatch sw = new Stopwatch();
sw.Start();
HashSet<string> missingEntities = new HashSet<string>();
Console.WriteLine("Validating...");
using (ExcelPackage xlPackage = new ExcelPackage(config.file))
{
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[config.WorkSheet];
// loop through each row
for (int iRow = config.DataRowStart; iRow <= config.DataRowFinish; iRow++)
{
// get home country from database
string homeCountryName = worksheet.Cell(iRow, config.HomeCountryColumnIndex).Value.Trim();
Guid? homeCountryId = null;
try
{
homeCountryId = ctx.Countries.AsNoTracking().Where(x => x.Name == homeCountryName).First().Id;
//Console.WriteLine(homeCountryId + " - " + homeCountryName);
}
catch (Exception ex)
{
//Console.WriteLine("No record found for " + homeCountryName);
missingEntities.Add(homeCountryName);
}
// get host country from database
string hostCountryName = worksheet.Cell(iRow, config.HostCountryColumnIndex).Value.Trim();
Guid? hostCountryId = null;
try
{
hostCountryId = ctx.Countries.AsNoTracking().Where(x => x.Name == hostCountryName).First().Id;
//Console.WriteLine(hostCountryId + " - " + hostCountryName);
}
catch (Exception ex)
{
//Console.WriteLine("No record found for " + hostCountryName);
missingEntities.Add(hostCountryName);
}
if (config.HomeLocationColumnIndex != null && config.HostLocationColumnIndex != null)
{
// get home country from database
string homeLocationName = worksheet.Cell(iRow, (int)config.HomeLocationColumnIndex).Value.Trim();
Guid? homeLocationId = null;
try
{
homeLocationId = ctx.Locations.AsNoTracking().Where(x => x.Name == homeLocationName).First().Id;
//Console.WriteLine(homeCountryId + " - " + homeCountryName);
}
catch (Exception ex)
{
//Console.WriteLine("No record found for " + homeLocationName);
missingEntities.Add(homeLocationName);
}
// get host country from database
string hostLocationName = worksheet.Cell(iRow, (int)config.HostLocationColumnIndex).Value.Trim();
Guid? hostLocationId = null;
try
{
hostLocationId = ctx.Locations.AsNoTracking().Where(x => x.Name == hostLocationName).First().Id;
//Console.WriteLine(hostCountryId + " - " + hostCountryName);
}
catch (Exception ex)
{
//Console.WriteLine("No record found for " + hostLocationName);
missingEntities.Add(hostLocationName);
}
}
}
}
Console.WriteLine("Validation complete...");
Console.WriteLine("Time elapsed: {0} minutes", sw.Elapsed.Minutes);
Console.WriteLine("No records found for:");
foreach (string entity in missingEntities)
{
Console.WriteLine(entity);
}
}
Here is the accompanying object:
public class ExcelConfiguration
{
public FileInfo file { get; set; }
public int HomeCountryColumnIndex { get; set; }
public int HostCountryColumnIndex { get; set; }
public int? HomeLocationColumnIndex { get; set; }
public int? HostLocationColumnIndex { get; set; }
public int WorkSheet { get; set; }
public int DataRowStart { get; set; }
public int DataRowFinish { get; set; }
}