Leading Project for Biosimulation > Cell/Biodynamics Simulation > simBio

Bug ID S-4: RelationGraph Runs Slowly


When a model xml file uses org.simBio.sim.analyzer.graph.RelationGraph, the simulation runs much more slowly.


The paintGraphSub method of RelationGraph takes increasingly longer amounts of time to run as the simulation continues. Fix this method so that RelationGraph runs as quickly as the other Graph classes.

Modified file list

  • org.simBio.sim.analyzer.graph.RelationGraph

Code explanation

RelationGraph was tested using the protocol xml file RelationGraphTest.make.xml in the folder src/test/resources/org/simBio/sim/analyzer/graph. In the original code, each time the paintGraphSub method was called it looped through all of the TimeSeriesValues from 0 until the current values, which took an ever increasing amount of time to complete, and slowed down the simulation.

long idxMin = 0;
long idxMax = values.getCurrentIndex();
for (long idx = idxMin; idx <= idxMax; idx++) {
	double x = axisX.p(values.getValue(idx, 0), plot, false);
	for (int i = nTargetOrigin; i < nTarget; i++) { 
		int nGraph = i - nTargetOrigin;
		drawPoint(plot, graphics2d, x, axisY.p(values.getValue(idx, i) * targetScale[i], plot, false), nGraph);

In the new version of the code, idxMin and idxMax have been changed from local variables to private fields

private long idxMin = 0;
private long idxMax = 0; 

The value of idxMax is assigned to idxMin, if the value of values.getCurrentIndex() is different to that of idxMax. Then values.getCurrentIndex() is assigned to idxMax.

if (idxMax != values.getCurrentIndex()) {
	idxMin = idxMax; 
idxMax = values.getCurrentIndex();

If the value assigned to idxMin is lower than zero, it is changed to 0. The loop then runs from idxMin, which was the previous value of idxMax, until the current value of idxMax. Therefore it runs much more quickly than the previous version, where the loop ran from 0 to idxMax each time paintGraphSub was called.

if (idxMin < 0) idxMin = 0;
for (long idx = idxMin; idx <= idxMax; idx++) { 

The next time paintGraphSub is called, the value of idxMax is assigned to idxMin again. Below is a comparison of the time taken for a simulation to run before and after the changes to RelationGraph.

Code changes Execution time
Before 343 seconds
After 64 seconds