Question
Bapu Hirave · Nov 13, 2016

To access object property value, performance wise whats fast method ?

Lets say i have a record "Person". If i want to access the "Name" property of that object which one of the following best option performance wise

1. Option 1

set record = ##class(Test.Person).%OpenId(1)

write record.Name

2. Option 2

 write ##class(Test.Person).NameGetStored(1)

0
0 289
Discussion (3)1
Log in or sign up to continue

The second option will be faster because we don't need to take in the whole object and put it into memory.  The first option does have to do that.  Here's a quick test that shows the second way is faster:

SAMPLES>s ts = $P($ZTS,",",2) f i=1:1:100000 { s name= ##class(Sample.Person).NameGetStored(1) } w "Time: "_(($P($ZTS,",",2))-ts)

Time: .139673

SAMPLES>s ts = $P($ZTS,",",2) f i=1:1:100000 { s p= ##class(Sample.Person).%OpenId(1) s name=p.Name } w "Time: "_(($P($ZTS,",",2))-ts)

Time: .504776

Now, if you want to go SUPER-fast, you can skip all this objects mumbo-jumbo and get that info right from the global:

SAMPLES>s ts = $P($ZTS,",",2) f i=1:1:100000 { s name = $LG(^Sample.PersonD(1),2)} w "Time: "_(($P($ZTS,",",2))-ts)

Time: .029287

However, this has no safeguards built in, and should only be used for your most dire of performance needs.  

Also this should be fast too:

Set id=1

&sql(select Name into :name from Test.Person where %ID=:id)

Write name,!