Speed up Live Plotting?
Posted: Wed Nov 11, 2009 7:28 am
Hello !
We have a problem with CPU consumption if we plot in a chart for a long time. Actually my data is generated by a timer which adds always one new point to each FastLineSeries. The timer interval can be configured : 10-1Sec, 0.5Sek, 0.25Sek, 0.2Sek, 0.1Sek.
If I start the application all works fine. We use some techniques to reduce the CPU usage (see code and explanation below). After starting the CPU usage is very low. Not more then 1% on a 450MHz system.
This is the code for adding data:
Ok let me explain the code a little bit ...
PlotInterval is a ComboBox with some entries. You can select how often the chart will be repaint.
And itemindex 11 means -> plot always if points are added.
if ChartFloating.Checked then begin
MDIChart.BottomAxis.Maximum := (MilliSec / 1000);
MDIChart.BottomAxis.Minimum := (MilliSec / 1000) - FloatingTime.Value;
end;
This code is used to float the x Axis. So the chart would only show the last X entries.
The naxt part in the code is for adding the new points to the series.
The next big part is used to reduce the count of repaints of the chart. A normal FastLineSeries would be repaint if a point is added. To avoid this we use this code to repaint only all x seconds. This reduces CPU usage extremly.
Ok so far the explanation to my code.
Now my problem. I have set the chart to display only the last 20 seconds of the chart. So the count of points which have to be drawn are always the same. And chart repainting is set to 10 seconds.
This works nice and it starts with a very low CPU usage. But after running the application over night I got a CPU usage by ~70%. In this time there where ~150000 points added to each TFastLineSeries.
So my question is ... Why is the CPU usage so high? I thought it would always be constant because we only draw the same amount of points to the chart.
Can you think of any improvements to have a constant CPU usage? I can´t imagine that the CPU usage get higher because of adding more data to the chart.
Hope you can give me a hint because this is a really big problem for long term data rcordings.
Greetz Dominik
We have a problem with CPU consumption if we plot in a chart for a long time. Actually my data is generated by a timer which adds always one new point to each FastLineSeries. The timer interval can be configured : 10-1Sec, 0.5Sek, 0.25Sek, 0.2Sek, 0.1Sek.
If I start the application all works fine. We use some techniques to reduce the CPU usage (see code and explanation below). After starting the CPU usage is very low. Not more then 1% on a 450MHz system.
This is the code for adding data:
Code: Select all
procedure TChild_Grafik.PlotTimerTimer(Sender: TObject);
var I, j : Integer;
MilliSec : Double;
PlotDiff : Integer;
Plot : Boolean;
begin
MilliSec := MilliSecondsBetween(Now, StartZeit);
MDIChart.AutoRepaint := PlotInterval.ItemIndex = 11;
if ChartFloating.Checked then begin
MDIChart.BottomAxis.Maximum := (MilliSec / 1000);
MDIChart.BottomAxis.Minimum := (MilliSec / 1000) - FloatingTime.Value;
end;
MDIChart[0].AddXY((MilliSec / 1000), MDIChart[0].Count + 1);
for I := 1 to MDIChart.SeriesCount - 1 do begin
MDIChart[I].AddXY((MilliSec / 1000), (Sin(MDIChart[I].MaxXValue / (10 * (I + 1))) +
Cos(MDIChart[I].MaxXValue / (30 * (I + 1)))
* ((I + 1) * 3) ))
end;
if PlotInterval.ItemIndex <> 11 then begin
PlotDiff := MilliSecondsBetween(Now, LastPlot);
case PlotInterval.ItemIndex of
// Plot every Hour
0 : Plot := PlotDiff >= 60000 * 60;
// Plot every 20 Minutes
1 : Plot := PlotDiff >= 60000 * 20;
// Plot every 10 Minutes
2 : Plot := PlotDiff >= 60000 * 10;
// Plot every 5 Minutes
3 : Plot := PlotDiff >= 60000 * 5;
// Plot every 2 Minutes
4 : Plot := PlotDiff >= 60000 * 2;
// Plot every Minute
5 : Plot := PlotDiff >= 60000;
// Plot every 20 seconds
6 : Plot := PlotDiff >= 20000;
// Plot every 10 seconds
7 : Plot := PlotDiff >= 10000;
// Plot every 5 seconds
8 : Plot := PlotDiff >= 5000;
// Plot every 2 seconds
9 : Plot := PlotDiff >= 2000;
// Plot every second
10 : Plot := PlotDiff >= 1000;
// Plot always
11 : Plot := True;
// Plot max. 2x / Sec
12 : Plot := PlotDiff >= 500;
// Plot max. 4x / Sec
13 : Plot := PlotDiff >= 250;
// Plot max. 10x / Sec
14 : Plot := PlotDiff >= 100;
end;
if Plot then begin
LastPlot := Now;
MDIChart.AutoRepaint := True;
MDIChart.Refresh;
end;
end;
end;
PlotInterval is a ComboBox with some entries. You can select how often the chart will be repaint.
And itemindex 11 means -> plot always if points are added.
if ChartFloating.Checked then begin
MDIChart.BottomAxis.Maximum := (MilliSec / 1000);
MDIChart.BottomAxis.Minimum := (MilliSec / 1000) - FloatingTime.Value;
end;
This code is used to float the x Axis. So the chart would only show the last X entries.
The naxt part in the code is for adding the new points to the series.
The next big part is used to reduce the count of repaints of the chart. A normal FastLineSeries would be repaint if a point is added. To avoid this we use this code to repaint only all x seconds. This reduces CPU usage extremly.
Ok so far the explanation to my code.
Now my problem. I have set the chart to display only the last 20 seconds of the chart. So the count of points which have to be drawn are always the same. And chart repainting is set to 10 seconds.
This works nice and it starts with a very low CPU usage. But after running the application over night I got a CPU usage by ~70%. In this time there where ~150000 points added to each TFastLineSeries.
So my question is ... Why is the CPU usage so high? I thought it would always be constant because we only draw the same amount of points to the chart.
Can you think of any improvements to have a constant CPU usage? I can´t imagine that the CPU usage get higher because of adding more data to the chart.
Hope you can give me a hint because this is a really big problem for long term data rcordings.
Greetz Dominik