Advanced TTCN-3 usage: the secret life of assignments [message #1745192] |
Thu, 06 October 2016 08:52 |
Kristof Szabados Messages: 60 Registered: July 2015 |
Member |
|
|
Is there a difference between assigning a structured value field by field, or using a structured notation?
According to the standard the end result should be the same ... but there is no mention about performance.
Lets do some measurements with simple records containing 3, 4, 5, 10 integer fields.
for example:
"
private type record record_with_3fields
{
integer field1,
integer field2,
integer field3
};
"
and do some assignment both field-by-field and in a structured style.
"
for (var integer i := 0; i < cg_loopCounter; i := i + 1) {
vl_temp3.field1 := 1;
vl_temp3.field2 := 2;
vl_temp3.field3 := 3;
}
...
for (var integer i := 0; i < cg_loopCounter; i := i + 1) {
vl_temp3 := {1,2,3};
}
"
We will see that assignments using structured notations are slightly faster.
In may case:
- for 3 fields: 0.35 seconds vs 0.31 seconds
- for 4 fields: 0.36 seconds vs 0.32 seconds
- for 5 fields: 0.43 seconds vs 0.36 seconds
- for 10 fields: 0.68 seconds vs 0.52 seconds
The structured assignment is always faster.
The reason behind this is, that when the assignment happens field-by-field additional code is generated for each statement, that tracks its location. While structure assignments are handled as a single assignment.
Please note, we can also observe that the duration of the operation depends on the amount of fields changed.
---
This kind of performance difference is still visible with partial assignments.
If I try to change some fields of a 5 field large record (after being initialized with a constant) I still see a similar effect:
- just assigning the constant in a loop: 0.42 seconds
- assigning the constant and changing all 5 fields field-by-field: 0.79 seconds
- assigning the constant and changing all 5 fields using structured notation: 0.72 seconds
- assigning the constant and changing 3 fields field-by-field: 0.68 seconds
- assigning the constant and changing 3 fields using structured notation: 0.65 seconds
as in "
vl_temp := cg_constant;
vl_temp := {1, - , 3, -, 5};
"
Here we see the exact same effect: the duration depends on the number of fields to be changed + structured notations are slightly faster.
---
What happens if the structure we wish to change is deeper (more structured types embedded into each other)?
Lets declare 2 record of types to encompass our 5 field simple record:
"
private type record of record_with_5fields recordOfRecords;
private type record of recordOfRecords recordOfROR;
"
and measure:
- field-by-field assignment ( "vl_temp[0][0].field1 := 1;" ) takes 0.79 seconds
- structured assignment (vl_temp := {{{1,2,3,4,5}}};) takes 0.51 seconds
We can see that structured assignment is not only faster than field-by-field assignment, but the difference is even greater compared to the 0.13 second difference seen in the earlier cases.
The reason behind this is how access to fields deeper in the structures is optimized.
While in the case of field-by-field assignment the execution has to "go to" each field for each assignment, in the structured form this can be optimised to "going to" the common path only once.
As in Titan each element/field/item access in the reference is translated into a functioncall, this optimization reduces the number of function calls needed to perform the value assignment significantly (in my case by about 35%)
---
To sum up: structured assignment notations have the same effect as field-by-field assignments, but usually perform faster.
/* and might also make the code more readable */
For the sake of completeness please note, that these operations are quite fast already in Titan: for such simple structures I had to use 10*1000*1000 iteration loops to overcome the effect of background operations and measurement errors.
|
|
|
Powered by
FUDForum. Page generated in 0.02415 seconds