I have a condition check logic, which seems to be taking more than 1/10 of CPU time (millions and millions are such checks are performed). I don’t think I can improve any other aspects of my solution, but at least this one may be something to be improved still.
Basically I have a loop inside a loop inside a loop, .. and then 3 such checks inside:
if (!Combination.CheckCondition(dateCondition1, profit1)) continue;
the implementation is following:
public static bool CheckCondition(Condition condition, decimal profit) { var conditionType = condition.ConditionType; if (conditionType == ConditionType.Disabled) { return true; } else if (conditionType == ConditionType.Between && profit >= condition.From && profit <= condition.To) { return true; } else if (conditionType == ConditionType.Bigger && profit >= condition.Exact) { return true; } else if (conditionType == ConditionType.Smaller && profit <= condition.Exact) { return true; } return false; }
Condition
class structure is following:
public class Condition { public static int CurrentIndex = 0; public int Id { get; set; } public ConditionType ConditionType { get; set; } public int From { get; set; } public int To { get; set; } public int Exact { get; set; } public Condition() { this.Id = CurrentIndex; CurrentIndex++; } } public enum ConditionType { None, Disabled, Smaller, Bigger, Between }
Overall loop picture is following:
var dateConditions = new List<Condition>() // this is just a small part of what I really have { new Condition() { ConditionType = ConditionType.Disabled }, new Condition() { ConditionType = ConditionType.Smaller, Exact = -70 }, new Condition() { ConditionType = ConditionType.Bigger, Exact = 0 }, new Condition() { ConditionType = ConditionType.Between, From = -30, To = 30 } }; Dictionary<(int, int, int, int, int, int, int, int, int), decimal> results = new Dictionary<(int, int, int, int, int, int, int, int, int), decimal>(); for (int date1End = 0 + 1; date1End < 10; date1End++) // Start with 0 (now), end with -24 hours, Step = 30 minutes { // logic to get profit1.. (depends on date1End) decimal profit1 = 12; foreach (var dateCondition1 in dateConditions) { if (!Combination.CheckCondition(dateCondition1, profit1)) continue; for (int date2End = 2 + 1; date2End < 9; date2End++) // start from -1 hour, end with -10 hours, step 1 hour { // logic to get profit2.. (depends on date2End) decimal profit2 = 5; foreach (var dateCondition2 in dateConditions) { if (!Combination.CheckCondition(dateCondition2, profit2)) continue; for (int date3End = 3 + 1; date3End < 8; date3End++) // start from -8 hours, end with -20 hours, step 1 hour { // logic to get profit3.. (depends on date3End) decimal profit3 = 7; foreach (var dateCondition3 in dateConditions) { if (!Combination.CheckCondition(dateCondition3, profit3)) continue; var calculatedAmount = 1234m; var key = new (0, date1End, 2, date2End, 3, date3End, dateCondition1.Id, dateCondition2.Id, dateCondition3.Id); if (!results.ContainsKey(key)) results.Add(key, calculatedAmount); } } } } } }
The aim of the program is to generate all possible code combinations for matching conditions for a given data. As I mentioned – this code is working perfectly fine, but there are so many checks performed in this one method, that I am starting to think of alternative solutions. I would appreciate of somebody had taken a look at my implementation and possibly gave some advices on how to improve the performance of the mentioned logic.
Currently it is taking more than 2 hours to run the code from the start to finish. Even 20 minutes improvement would be great.
If this is not the correct place to ask – please let me know.