You could use use Sys.getTime() and check the time it takes to run each variation a 1000 times or something and see the difference, or I know Travis has demonstrated how to show the byte code generated for each line which also might give you some info. (I think it's an option to the compiler) But step back a bit - what is it you're trying to correct? Is it just that you want to make the code as fast as possible, or is it a specific problem?
using Toybox.Application as App;
using Toybox.Graphics as Gfx;
using Toybox.WatchUi as Ui;
using Toybox.System as Sys;
class MyView extends Ui.View
{
hidden static const _tests = [
:testNumberI_Plus_I_Div_2,
:testLongI_Plus_I_Div_2,
:testNumberI_Plus_I_RShift_1,
:testLongI_Plus_I_RShift_1,
:testNumberI_PlusEqual_I_Div_2,
:testLongI_PlusEqual_I_Div_2,
:testNumberI_PlusEqual_I_RShift_1,
:testLongI_PlusEqual_I_RShift_1
];
hidden static const _rows = [
"",
"A",
"B",
"C",
"D"
];
hidden static const _cols = [
"",
"N",
"L"
];
hidden var _mode;
hidden var _operations;
hidden var _milliseconds;
hidden const _iterations = 1000;
hidden const _min_milliseconds = 500;
function initialize() {
View.initialize();
_mode = 0;
_operations = new [ _tests.size() ];
_milliseconds = new [ _tests.size() ];
for (var i = 0; i < _tests.size(); ++i) {
_operations= 0;
_milliseconds= 0;
}
}
function testNumberI_Plus_I_Div_2() {
return testI_Plus_I_Div_2(1, 1);
}
function testNumberI_Plus_I_RShift_1() {
return testI_Plus_I_Div_2(1, 1);
}
function testNumberI_PlusEqual_I_Div_2() {
return testI_PlusEqual_I_Div_2(1, 1);
}
function testNumberI_PlusEqual_I_RShift_1() {
return testI_PlusEqual_I_RShift_1(1, 1);
}
function testLongI_Plus_I_Div_2() {
return testI_Plus_I_Div_2(1l, 1l);
}
function testLongI_Plus_I_RShift_1() {
return testI_Plus_I_RShift_1(1l, 1l);
}
function testLongI_PlusEqual_I_Div_2() {
return testI_PlusEqual_I_Div_2(1l, 1l);
}
function testLongI_PlusEqual_I_RShift_1() {
return testI_PlusEqual_I_RShift_1(1l, 1l);
}
function testI_Plus_I_Div_2(x, y) {
for (var i = 0; i < _iterations; ++i) {
x = x + x / 2;
x = x + x / 2;
x = x + x / 2;
x = x + x / 2;
x = x + x / 2;
}
return _iterations * 5;
}
function testI_Plus_I_RShift_1(x, y) {
for (var i = 0; i < _iterations; ++i) {
x = x + x >> 1;
x = x + x >> 1;
x = x + x >> 1;
x = x + x >> 1;
x = x + x >> 1;
}
return _iterations * 5;
}
function testI_PlusEqual_I_Div_2(x, y) {
for (var i = 0; i < _iterations; ++i) {
x += x / 2;
x += x / 2;
x += x / 2;
x += x / 2;
x += x / 2;
}
return _iterations * 5;
}
function testI_PlusEqual_I_RShift_1(x, y) {
for (var i = 0; i < _iterations; ++i) {
x += x >> 1;
x += x >> 1;
x += x >> 1;
x += x >> 1;
x += x >> 1;
}
return _iterations * 5;
}
hidden var _timer;
function onShow() {
_timer = new Timer.Timer();
_timer.start(self.method(:onTimer), 1000, true);
}
function onHide() {
_timer.stop();
_timer = null;
}
function onTimer() {
var mode = _mode % _tests.size();
_mode += 1;
var operations = 0;
var milliseconds = 0;
var a = Sys.getTimer();
do {
operations += method(_tests[mode]).invoke();
milliseconds = (Sys.getTimer() - a);
} while (milliseconds < _min_milliseconds);
_operations[mode] += operations;
_milliseconds[mode] += milliseconds;
Ui.requestUpdate();
}
hidden function value_for_cell(row, col) {
if (row == 0) {
return _cols[col];
}
else if (col == 0) {
return _rows[row];
}
row -= 1;
col -= 1;
var cell = row * (_cols.size() - 1) + col;
if (cell < _milliseconds.size() && _milliseconds[cell] != 0) {
return (_operations[cell] / _milliseconds[cell]).format("%4d");
}
return "-";
}
function onUpdate(dc) {
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.clear();
var rows = _rows.size();
var cols = _cols.size();
var font = Gfx.FONT_XTINY;
var d = dc.getTextDimensions("0123456789", font);
var dx = d[0] * 5 / 10.0; // average width of 5 digit
var dy = d[1] * 1.0; // height of 1 digit
var x = dc.getWidth() / 2;
var y = dc.getHeight() / 2;
x -= (dx * (cols - 1)) / 2;
y -= (dy * (rows - 0)) / 2;
for (var row = 0; row < rows; ++row) {
var yj = y + (row * dy);
for (var col = 0; col < cols; ++col) {
var xj = x + (col * dx);
var value = value_for_cell(row, col);
if (value != null) {
dc.drawText(xj, yj, font, value, Gfx.TEXT_JUSTIFY_RIGHT);
}
}
}
}
}
class ZZZApp extends App.AppBase
{
function initialize() {
AppBase.initialize();
}
function getInitialView() {
return [ new MyView() ];
}
}
[/code]
When I run this on my 735xt, I see about 40 ops/ms with Lang.Number, and 15 ops/ms with Lang.Long.using Toybox.Application as App;
using Toybox.Graphics as Gfx;
using Toybox.WatchUi as Ui;
using Toybox.System as Sys;
var _global = 0;
class MyView extends Ui.View
{
hidden static const _tests = [
:test_read_global,
:test_write_global,
:test_read_global_explicit,
:test_write_global_explicit,
:test_read_class,
:test_write_class,
:test_read_class_explicit,
:test_write_class_explicit,
:test_read_local,
:test_write_local
];
hidden static const _rows = [
"",
"G",
"GE",
"C",
"CE",
"L"
];
hidden static const _cols = [
"",
"R",
"W"
];
hidden var _mode;
hidden var _operations;
hidden var _milliseconds;
hidden const _iterations = 1000;
hidden const _min_milliseconds = 500;
hidden var _member = 0;
function initialize() {
View.initialize();
_mode = 0;
_operations = new [ _tests.size() ];
_milliseconds = new [ _tests.size() ];
for (var i = 0; i < _tests.size(); ++i) {
_operations= 0;
_milliseconds= 0;
}
}
function test_read_global_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
}
return _iterations * 10;
}
function test_write_global_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
}
return _iterations * 10;
}
function test_read_global(x) {
for (var i = 0; i < _iterations; ++i) {
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
}
return _iterations * 10;
}
function test_write_global(x) {
for (var i = 0; i < _iterations; ++i) {
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
}
return _iterations * 10;
}
function test_read_class_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
}
return _iterations * 10;
}
function test_write_class_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
}
return _iterations * 10;
}
function test_read_class(x) {
for (var i = 0; i < _iterations; ++i) {
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
}
return _iterations * 10;
}
function test_write_class(x) {
for (var i = 0; i < _iterations; ++i) {
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
}
return _iterations * 10;
}
function test_read_local(x) {
var _local = 0;
for (var i = 0; i < _iterations; ++i) {
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
}
return _iterations * 10;
}
function test_write_local(x) {
var _local = 0;
for (var i = 0; i < _iterations; ++i) {
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
}
return _iterations * 10;
}
hidden var _timer;
function onShow() {
_timer = new Timer.Timer();
_timer.start(self.method(:onTimer), 1000, true);
}
function onHide() {
_timer.stop();
_timer = null;
}
function onTimer() {
var mode = _mode % _tests.size();
_mode += 1;
var operations = 0;
var milliseconds = 0;
var a = Sys.getTimer();
do {
operations += method(_tests[mode]).invoke(1);
milliseconds = (Sys.getTimer() - a);
} while (milliseconds < _min_milliseconds);
_operations[mode] += operations;
_milliseconds[mode] += milliseconds;
Ui.requestUpdate();
}
hidden function value_for_cell(row, col) {
if (row == 0) {
return _cols[col];
}
else if (col == 0) {
return _rows[row];
}
row -= 1;
col -= 1;
var cell = row * (_cols.size() - 1) + col;
if (cell < _milliseconds.size() && _milliseconds[cell] != 0) {
return (_operations[cell] / _milliseconds[cell]).format("%4d");
}
return "-";
}
function onUpdate(dc) {
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.clear();
var rows = _rows.size();
var cols = _cols.size();
var font = Gfx.FONT_XTINY;
var d = dc.getTextDimensions("0123456789", font);
var dx = d[0] * 5 / 10.0; // average width of 5 digit
var dy = d[1] * 1.0; // height of 1 digit
var x = dc.getWidth() / 2;
var y = dc.getHeight() / 2;
x -= (dx * (cols - 1)) / 2;
y -= (dy * (rows - 0)) / 2;
for (var row = 0; row < rows; ++row) {
var yj = y + (row * dy);
for (var col = 0; col < cols; ++col) {
var xj = x + (col * dx);
var value = value_for_cell(row, col);
if (value != null) {
dc.drawText(xj, yj, font, value, Gfx.TEXT_JUSTIFY_RIGHT);
}
}
}
}
}
class ZZZApp extends App.AppBase
{
function initialize() {
AppBase.initialize();
}
function getInitialView() {
return [ new MyView() ];
}
}
[/code]using Toybox.Application as App;
using Toybox.Graphics as Gfx;
using Toybox.WatchUi as Ui;
using Toybox.System as Sys;
var _global = 0;
var mode=0;
class MyView extends Ui.View
{
hidden static const _tests = [
:test_nil,
:test_nil,
:test_read_global,
:test_write_global,
:test_read_global_explicit,
:test_write_global_explicit,
:test_read_class,
:test_write_class,
:test_read_class_explicit,
:test_write_class_explicit,
:test_read_local,
:test_write_local
];
hidden static const _rows = [
"",
"nil",
"G",
"GE",
"C",
"CE",
"L"
];
hidden static const _cols = [
"",
"R",
"W"
];
hidden var _mode;
hidden var _operations;
hidden var _milliseconds;
hidden const _iterations = 1000;
hidden const _min_milliseconds = 250;
hidden var _member = 0;
function initialize() {
View.initialize();
_mode = 0;
_operations = new [ _tests.size() ];
_milliseconds = new [ _tests.size() ];
for (var i = 0; i < _tests.size(); ++i) {
_operations= 0;
_milliseconds= 0;
}
}
function test_read_global_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
x = $._global + 1;
}
return _iterations * 10;
}
function test_write_global_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
$._global = x + 1;
}
return _iterations * 10;
}
function test_read_global(x) {
for (var i = 0; i < _iterations; ++i) {
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
x = _global + 1;
}
return _iterations * 10;
}
function test_write_global(x) {
for (var i = 0; i < _iterations; ++i) {
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
_global = x + 1;
}
return _iterations * 10;
}
function test_read_class_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
x = self._member + 1;
}
return _iterations * 10;
}
function test_write_class_explicit(x) {
for (var i = 0; i < _iterations; ++i) {
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
self._member = x + 1;
}
return _iterations * 10;
}
function test_read_class(x) {
for (var i = 0; i < _iterations; ++i) {
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
x = _member + 1;
}
return _iterations * 10;
}
function test_write_class(x) {
for (var i = 0; i < _iterations; ++i) {
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
_member = x + 1;
}
return _iterations * 10;
}
function test_read_local(x) {
var _local = 0;
for (var i = 0; i < _iterations; ++i) {
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
x = _local + 1;
}
return _iterations * 10;
}
function test_write_local(x) {
var _local = 0;
for (var i = 0; i < _iterations; ++i) {
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
_local = x + 1;
}
return _iterations * 10;
}
function test_nil(x)
{
for (var i = 0; i < _iterations; ++i) { }
return _iterations * 10;
}
hidden var _timer;
function onShow() {
_timer = new Timer.Timer();
_timer.start(self.method(:onTimer), 1000, true);
}
function onHide() {
_timer.stop();
_timer = null;
}
function onTimer() {
var mode = _mode % _tests.size();
_mode += 1;
var operations = 0;
var milliseconds = 0;
var a = Sys.getTimer();
do {
operations += method(_tests[mode]).invoke(1);
milliseconds = (Sys.getTimer() - a);
} while (milliseconds < _min_milliseconds);
_operations[mode] += operations;
_milliseconds[mode] += milliseconds;
Ui.requestUpdate();
}
hidden function value_for_cell(row, col) {
if (row == 0) {
return _cols[col];
}
else if (col == 0) {
return _rows[row];
}
row -= 1;
col -= 1;
var cell = row * (_cols.size() - 1) + col;
if (cell < _milliseconds.size() && _milliseconds[cell] != 0)
{ var time=_milliseconds[cell]*10000/_operations[cell];
if (mode)
{ cell=cell&1;
time-=_milliseconds[cell]*10000/_operations[cell];
}
return (time/10)+"."+(time%10)+"ms";
}
return "-";
}
function onUpdate(dc) {
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.clear();
var rows = _rows.size();
var cols = _cols.size();
var font = Gfx.FONT_XTINY;
var d = dc.getTextDimensions("0123456789", font);
var dx = d[0] * 7 / 10.0; // average width of 5 digit
var dy = d[1] * 1.0; // height of 1 digit
var x = dc.getWidth() / 2;
var y = dc.getHeight() / 2;
x -= (dx * (cols - 1)) / 2;
y -= (dy * (rows - 0)) / 2;
for (var row = 0; row < rows; ++row) {
var yj = y + (row * dy);
for (var col = 0; col < cols; ++col) {
var xj = x + (col * dx);
var value = value_for_cell(row, col);
if (value != null) {
dc.drawText(xj, yj, font, value, Gfx.TEXT_JUSTIFY_RIGHT);
}
}
}
}
}
class MyBehaviour extends Ui.BehaviorDelegate
{
function initialize() { Ui.BehaviorDelegate.initialize(); }
function onMenu() { return true; }
function onSelect() { mode^=1; Ui.requestUpdate(); return true; }
}
class Test extends App.AppBase
{
function initialize() { AppBase.initialize(); }
function getInitialView() { return [new MyView(), new MyBehaviour()]; }
}[/CODE]