Nasal performance and benchmarks: Difference between revisions

From FlightGear wiki
Jump to navigation Jump to search
(add test descriptions and first results)
mNo edit summary
Line 28: Line 28:
For most real-world cases this should be neglectible, nevertheless it is a simple, easy to understand test scenario.
For most real-world cases this should be neglectible, nevertheless it is a simple, easy to understand test scenario.


<nowiki>
<syntaxhighlight lang="nasal">
var simpleClass = {
var simpleClass = {
     new: func {
     new: func {
Line 56: Line 56:
     c.value = 1;
     c.value = 1;
}
}
</nowiki>
</syntaxhighlight>


=== property tree access ===
=== property tree access ===
Line 62: Line 62:
This benchmarks compares the getprop() agains the props.Node.getValue().
This benchmarks compares the getprop() agains the props.Node.getValue().


<nowiki>
<syntaxhighlight lang="nasal">
var myprops = [];
var myprops = [];
var path = "/_benchmark/getprop/prop";
var path = "/_benchmark/getprop/prop";
Line 85: Line 85:
printResult("test_getvalue", debug.benchmark_time(test_getvalue, 1));
printResult("test_getvalue", debug.benchmark_time(test_getvalue, 1));
printResult("test_getprop", debug.benchmark_time(test_getprop, 1));
printResult("test_getprop", debug.benchmark_time(test_getprop, 1));
</nowiki>
</syntaxhighlight>


=== Canvas API - Text class ===
=== Canvas API - Text class ===

Revision as of 14:19, 23 January 2019

This article is a stub. You can help the wiki by expanding it.

This article shall collect best practices for coding in Nasal based on regular performance tests.

Motivation

Over the years many things have been improved so some old findings regarding performance are not true anymore. Regular benchmarking and updating should help the FlightGear community to have a clear picture of which code constructs perform well. Sometimes performance and "good" coding style (e.g. readability, re-usability, maintainability) might be in conflict.

Benchmark add-on

The benchmark add-on can be downloaded here. It is basically a collection of nasal snippets to be tested. A minimal GUI (menu item, simple dialog box) allows to start some benchmarks.

A benchmark test runs some function multiple time (repeat count) and measures the time taken (result). The results of any benchmark is currently printed to the console, so you have to start FlightGear with console window to see this. (The repeat count should be configurable via the GUI, this is on my to do list.)

Performance analysis

Performance of Nasal snippets is tested by means of the Benchmark add-on which makes use of the debug.benchmark_time() function. The default repeat count for tests is 10000 (ten thousand). Depending on your hardware some tests may block your Flightgear many seconds. On Windows you may see the usual warnings about FlightGear not reacting any more. In the next section available benchmarks are described. Test results follow below.

Results (time taken to complete a benchmark test) obviously depend on the hardware used, so the relevant information is not the absolute time but the ratio when comparing two code snippets. Also, one should repeat benchmarks a couple of times and see if results are in the same order of magnitude. Benchmarks should be run only after FlightGear is fully ready, e.g. scenery and aircraft model loaded etc.

currently available benchmarks

simpleClass

Object oriented programming (OOP, "programming with classes") gives several advantages (readability, re-usability and easier maintainance of code, to give just some examples) but might generate slightly slower code. For most real-world cases this should be neglectible, nevertheless it is a simple, easy to understand test scenario.

var simpleClass = {
    new: func {
        var obj = {
            parents: [simpleClass],
            value: 0,
        };
        return obj;
    },
    
    set: func(value) { me.value = value; },
    get: func { return me.value; },
};

var c = simpleClass.new();

var test_get_method = func {
    var x = c.get();
}
var test_get_direct = func {
    var x = c.value;
}
var test_set_method = func {
    c.set(1);
}
var test_set_direct = func {
    c.value = 1;
}

property tree access

As the property tree is the central data exchange in FlightGear it is worth to know how to efficiently access it. This benchmarks compares the getprop() agains the props.Node.getValue().

var myprops = [];
var path = "/_benchmark/getprop/prop";
var test_getvalue = func {
    var test = 0;
    forindex (i; myprops) {
        t = myprops[i].getValue();
    }
}
var test_getprop = func {
    var test = 0;
    forindex (i; myprops) {
        t = getprop(path~i);
    }
}
var init = func {
    for (var i = 0; i < repeat; i += 1) {
        append(myprops, props.getNode(path~i, 1));
    }
}
printResult("test_getNode", debug.benchmark_time(init, 1));
printResult("test_getvalue", debug.benchmark_time(test_getvalue, 1));
printResult("test_getprop", debug.benchmark_time(test_getprop, 1));

Canvas API - Text class

tbd

Results 2019-01

simpleClass

Repeat count = 10000

Interpretation: using class methods is fine.

Test Time [s] Comment
get via class.get() 0.015
get via class.variable 0.006
set via class.set() 0.011
set via class.variable 0.004

Property access

Repeat count = 10000

It seems that any operation involving a property path (string) is relativly slow. If you have to access a property more than once, e.g. in an update loop, you should create a props.Node object and use object.getValue().

Test Time [s] Comment
props.getNode() 0.420
node.getValue() 0.010 using a node object is 36-40 times faster
getprop() 0.368

Canvas API - Text class

Optimized canvas API with caching and props.Node (not merged to FGDATA at the time of writing)

Test Time [s] Comment
setText() same value 0.015 old: 0.075
setText() different value 0.100 old: 0.150
appendText 9.0 the ~ operator is really slow

References

Regression tests

Articles

See also

Discussions

References