Poor Recalculate Performance when inserting Manual Tasks

Hi,

I am using Aspose.Tasks 6.2. In the attachment you can find sample code that is inserting 20 automatic tasks and 20 manual tasks into an empty project file. When the code reaches the line that recalculate Tasks it takes so long to complete.

Hi Ali,

I have analyzed the requirement and would like to share that its expected behavior of MSP as well that when number of tasks increases and we call CalculateProject, it takes longer time. You may verify this behavior by running the following macro which adds tasks and calls CalculateProject.


Sub TestMacro()
' Macro
    Dim proj As Project
    Dim tsk As task
Set proj = Application.ActiveProject
proj.NewTasksCreatedAsManual = False
Dim start As Date
    start = Time
Dim t As task
For i = 1 To 500
Set t = proj.Tasks.Add("Task " & i)
t.start = DateAdd("d", i + 1, t.start)
Application.CalculateProject
t.Duration = i* 480
Application.CalculateProject
t.Finish = DateAdd("d", i + 1, t.start)
Application.CalculateProject
    Next i
Debug.Print Format(Time - start, "hh:mm:ss")
End Sub

For better performance, it is suggested that you may add tasks manually by setting the Project.CalculateafterEdit = false and then call Recalculate. Following is a sample code which adds 20 tasks manually and then calls Recalculate while taking very short time. Could you please give a try to the following sample code and let us know your feedback?

Project prj = new Project();
prj.CalculateAfterEdit = false;
string TaskName;
// Create new stopwatch
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
for (int i = 0; i < 20; i++)
{
    TaskName = "Task " + i;
    Task tempTask = prj.AddTask(TaskName);
    tempTask.Start = tempTask.ConstraintDate = DateTime.Now.Date + new TimeSpan(8, 0, 0);
    tempTask.ConstraintType = ConstraintType.StartNoEarlierThan;
    tempTask.Duration = new TimeSpan((i + 1) * 8, 0, 0);
    tempTask.Finish = tempTask.Start.AddDays(i + 1);
    tempTask.ManualStart = tempTask.Start;
    tempTask.ManualDuration = tempTask.Duration;
    tempTask.ManualFinish = tempTask.Finish;
    tempTask.IsManual = true;
    Console.WriteLine(i + " Start=" + tempTask.Start.ToString() + ", Finish=" + tempTask.Finish.ToString() + ", Duration=" + tempTask.Duration.ToString());
}
prj.UpdateReferences();
prj.CalcTaskIds();
prj.CalcTaskUids();
prj.CalcSlacks();
// Set early/late dates and slacks
Task.Recalculate(prj.RootTask);
// Stop timing
stopwatch.Stop();
// Write result
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
//Recalculate(prj);
prj.Save(@“D:\Result.xml”, SaveFileFormat.XML);

Your solution has better performance but this issue is not happening for automatic tasks. When I insert 5000 automatic tasks it takes almost 900 ms to recalculate but when I insert 5000 manual tasks it takes more than 30 minutes to complete.

Hi Ali,


We have investigated the issue further and have noticed that for this example, the duration of tasks towards the end of the loop reach approximately 12-13 years long, and project decomposition must be made in a better way at this stage. Though we compare the performance of our API with Microsoft Project (MSP), we have noticed that the same operation using MSP takes almost 18 minutes. Considering the difference in the time taken, there is some room for improvement in project recalculation which we are currently investigating. We’ll update you here as soon as our investigation is complete and request you to spare us some time in this regard.