what's the difference between MATREAD and READ?
Asked Answered
R

2

5

I need to understand the difference between MATREAD and READ? and please provide a simple example on where and how to use one.

Rigorism answered 13/5, 2013 at 11:38 Comment(0)
A
9

READ will read a record from disk and return it as a Dynamic Array. MATREAD will read a record from disk and return it as a Dimensioned Array.

So, the real trick is to work out which array type is more applicable for your use case.

A Dynamic Array is essentially a string which uses certain byte markers to delimit each element (attribute/multivalue/subvalue). It is extremely easy to use, doesn't require upfront declaration or sizing. On the downside, if can be slower than a Dimensioned Array, mainly for large records or when you will be randomly retrieve attributes from the array - O(n log n) I think. Sequential access is optimized and is close to a Dimensioned Arrays speed.

A Dimensioned Array is essentially an array of strings (or Dynamic Arrays in the case of UniVerse). It reads each attribute into an array position. Each array position will then consist of a Dynamic Array for just the multivalue/subvalue positions of that attribute. Dimensioned Arrays required you to declare them and the number of array positions upfront. Depending on the flavor you have running, it will may cause a runtime error if you attempt to read in a record with more attributes than the array is sized for. On the flip side, it is O(1) to retrieve attributes from the array, regardless of your access pattern.

Averyaveryl answered 13/5, 2013 at 13:25 Comment(3)
Dan - good explanation. I think dynamic array access is O(n), however. Whipped up a quick test and below are the results: Time to access position: 50000 = 0 ms. Time to access position: 100000 = 1 ms. Time to access position: 200000 = 3 ms. Time to access position: 500000 = 8 ms. Time to access position: 1000000 = 15 ms. Time to access position: 5000000 = 76 ms. Time to access position: 10000000 = 153 ms. The last couple examples show a pretty good linear progression.Survive
Thanks Shawn - you are correct. A single access is O(n). I was thinking about accessing all elements, which is O(n) for dimensioned and O(n^2) for dynamic.Averyaveryl
Dan - just wanted to add this is a great answer. Very direct and well informed.Pulsation
C
2

MATREADU bundles a MATPARSE with the READ. These blow the attributes of a dynamic array into a dimensioned or fixed array. This may be more efficient if you access the array frequently and more convenient if you are reading an I-type from a DICTionary. READV reads only one array attribute. Aim for clarity and optimize as required.

Here is an example in UniVerse of 4 lines producing the same output:

dim dimarr1(9), dimarr2(9)                                      
open 'VOC' else abort                                           
matread dimarr1 from 'OLDSTYLE' then print dimarr1(1) else abort
read dynarr from 'OLDSTYLE' then print dynarr<1> else abort     
matparse dimarr2 from dynarr ; print dimarr2(1)                 
readv dynatt from 'OLDSTYLE', 1 then print dynatt else abort    
end 
Cayla answered 13/5, 2013 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.