Creating a Garmin watch face: performance 4

Tips for improving performance.

See Creating a Garmin watch face: performance.

After that, I also checked the forum to see if the above measurement was correct.
The forum stated that System.getTimer() would be used, but even when I actually tried it on the simulator, the processing time was 0 ms, and I could not determine whether it was correct or incorrect.

Techniques for improving performance.

Reduce the number of operations.

When performing the same operation more than once, it is better to assign the operation to a variable once and use it.
Looking at the performance, up to two times, if the same calculation was performed three or more times by using the value assigned to the variable, the performance improved by assigning to the variable.

Ordinary pattern

First, Creating a Garmin watch face: performance 3, but the following coding.

for (var i = 1; i < 180; i++) {
	var now = [center[0] + radius * Math.cos(baseAngle * i), center[1] - radius * Math.sin(baseAngle * i)];
	dc.drawLine(prev[0], prev[1], now[0], now[1]);
	prev = now; 
}

The angle given to sin / cos is performed each time each function is called.
This is common code, but in such a case, it is better to assign it to a variable once and then pass it to sin / cos.

for (var i = 1; i < 180; i++) {
	var angle = baseAngle * i;
	var now = [center[0] + radius * Math.cos(angle), center[1] - radius * Math.sin(angle)];
	dc.drawLine(prev[0], prev[1], now[0], now[1]);
	prev = now; 
}

Pattern used for judgment in for statement

The same thing is often done about the comparison part of the for statement.
If an implementation that performs an operation is performed in this part, the performance will be reduced.

a = 5;
b = 4;
for (var i = 1; i < a * b; i++) {
	System.println(i);
}

In this case, since the operation of a * b is performed every time at the part of i <a * b, the performance is reduced.
Assign a * b to a variable before the for statement, and compare it with it.

Degree radian conversion

Some functions are inconsistent with degrees and radians.
I think it is better to do as follows.

var toRad = Math.PI / 180.0;
radian = degree * toRad;
degree = radian / toRad;

A method of preparing constants for degree-radian conversion in toRad and calculating using them.
Math had a conversion function, but the above implementation was about 10ns faster.

Drawing process

Drawing text

Drawing text took longer as the font size was larger.
The processing time was the same, but the graphics time was different.

Drawing an arc

This is because, as shown in Creating a Garmin watch face: performance 3, the overall processing time was faster when drawing and decomposing into line segments using drawLine instead of drawArc.

Finally

The performance enhancements listed here are based on the assumption that Monkey C has no compiler optimizations. It could not be determined whether the optimization could be performed.

If optimizations become available in the future, such implementation techniques may not be needed.

コメント

Copied title and URL