First, measuring execution times on modern operating systems where multiple processes run in parallel (on multiple CPUs) is challenging. The following demo application assigns a value to a variable in four different ways:
– in a single method
– in two methods, both in the same class
– in two methods where one method code is in an inherited class, and
– in two methods where one method is in a different class
As expected, the first is the fastest (keyword: loop unrolling) and the last is the slowest, the other two take about the same time.
Class DC.Times Extends (%RegisteredObject, TimesAbstract)
{
ClassMethod ShowTimes()
{
while $zh#1 {} set t1=$zh for i=1:1:1E6 { do ..Complete() } set t1=$zh-t1
while $zh#1 {} set t2=$zh for i=1:1:1E6 { do ..OneClass() } set t2=$zh-t2
while $zh#1 {} set t3=$zh for i=1:1:1E6 { do ..InhClass() } set t3=$zh-t3
while $zh#1 {} set t4=$zh for i=1:1:1E6 { do ..TwoClass() } set t4=$zh-t4
write $j(t1,9,5), $j(t2,9,5), $j(t3,9,5), $j(t4,9,5),!
}
/// The complete application is carried out in one method
ClassMethod Complete()
{
set x=12345
set y=12345
}
/// The entire application is done in the same class, but with different methods
/// Both methods are local (OneClass + LocTask)
ClassMethod OneClass()
{
set x=..LocTask()
set y=..LocTask()
}
/// The entire application is done in the same class, but with different methods
/// One method is local (InhClass) the other is inherited (InhTask)
ClassMethod InhClass()
{
set x=..InhTask()
set y=..InhTask()
}
/// The entire application uses two methods in two different classes
ClassMethod TwoClass()
{
set x=##class(DC.Times2).ExtTask()
set y=##class(DC.Times2).ExtTask()
}
/// As an "application" we simply return a constant value
ClassMethod LocTask(val)
{
quit 12345
}
}
Class DC.Times2 Extends %RegisteredObject
{
/// As an "application" we simply return a constant value
ClassMethod ExtTask(val)
{
quit 12345
}
}
Class DC.TimesAbstract [ Abstract ]
{
/// As an "application" we simply return a constant value
ClassMethod InhTask(val)
{
quit 12345
}
}Some time values
USER>
USER>f i=1:1:3 d ##class(DC.Times).ShowTimes()
0.10833 0.19660 0.19649 0.22001
0.10837 0.19657 0.19608 0.22000
0.10826 0.19661 0.19603 0.21992
USER>
USER>f i=1:1:3 d ##class(DC.Times).ShowTimes()
0.10998 0.19711 0.19643 0.22006
0.10830 0.19657 0.19624 0.22013
0.10822 0.19684 0.19628 0.22139
USER>
USER>w $zv
IRIS for UNIX (Ubuntu Server 22.04 LTS for x86-64) 2025.1 (Build 225_1U) Fri May 16 2025 12:18:04 EDT
USER>
Second, the choice of development method (use of include files, class inheritance, multiple classes, a large method in an even larger class, etc.) depends on other factors such as maintainability, runtime priority, etc.
- Log in to post comments