discuss@lists.openscad.org

OpenSCAD general discussion Mailing-list

View all threads

Determining the dimensions of an array

M
MichaelAtOz
Mon, Feb 21, 2022 7:18 AM

Re performance, I wonder whether OpenSCAD maintains a list size as part of the variables (internal) structure, or if len(list) runs through the list to count them.

I went and looked, it uses toVector().size(), in gcc run-time that basically does offset pointers last-first so it doesn't iterate.

So looping through a list of lists to get each size is O(n).


From: Jordan Brown [mailto:openscad@jordan.maileater.net]
Sent: Mon, 21 Feb 2022 14:37
To: OpenSCAD general discussion; Bob Ewart
Subject: [OpenSCAD] Re: Determining the dimensions of an array

Do be aware that OpenSCAD does not have 2D arrays or 3D arrays.

It has lists, and each list element may be different.  Yes, if you have a list of N elements, each of which is a list of M elements, that's a lot like an NxM array.

But this is equally legal:

[
1,
[2, 3],
[
[4,5,6],
[7,8,9]
]
]

along with other combinations that are arbitrarily more complex.

If you know that your list and its elements form a simple rectangular array then you can answer questions about dimensions and dimensionality, but in the general case it's hard to figure out what the questions even mean.

--
This email has been checked for viruses by AVG.
https://www.avg.com

Re performance, I wonder whether OpenSCAD maintains a list size as part of the variables (internal) structure, or if len(list) runs through the list to count them. I went and looked, it uses toVector().size(), in gcc run-time that basically does offset pointers last-first so it doesn't iterate. So looping through a list of lists to get each size is O(n). _____ From: Jordan Brown [mailto:openscad@jordan.maileater.net] Sent: Mon, 21 Feb 2022 14:37 To: OpenSCAD general discussion; Bob Ewart Subject: [OpenSCAD] Re: Determining the dimensions of an array Do be aware that OpenSCAD does not have 2D arrays or 3D arrays. It has lists, and each list element may be different. Yes, if you have a list of N elements, each of which is a list of M elements, that's a lot like an NxM array. But this is equally legal: [ 1, [2, 3], [ [4,5,6], [7,8,9] ] ] along with other combinations that are arbitrarily more complex. If you know that your list and its elements form a simple rectangular array then you can answer questions about dimensions and dimensionality, but in the general case it's hard to figure out what the questions even mean. -- This email has been checked for viruses by AVG. https://www.avg.com
AM
Adrian Mariano
Mon, Feb 21, 2022 11:30 AM

Well, in this case, the constants dominate over the asymptotic performance,
which is often the case in OpenSCAD, it seems.  Running list_shape on a
1000x1000 matrix takes 0.96 seconds, which is pretty darn slow.  In
comparison, running is_matrix() takes 0.043 seconds, so it's 22 times
faster in this case.  Once you know it has matrix shape you can then do
len(M) and len(M[0]) to get dimensions.  What is_matrix does is to
construct a list of zeros whose length matches the length of M[0] and then
it checks if each entry in M satisfies M[i]*0 == list_of_zeros.  This is a
little different than using list_shape for the same test because is_matrix
will only return true on numeric inputs.  The is_matrix() function is much
faster because it doesn't use recursion and it relies on OpenSCAD to check
an entire row at a time, so the list comprehension is only across on
dimension of the data structure.

On Mon, Feb 21, 2022 at 2:18 AM MichaelAtOz oz.at.michael@gmail.com wrote:

Re performance, I wonder whether OpenSCAD maintains a list size as part of
the variables (internal) structure, or if len(list) runs through the list
to count them.

I went and looked, it uses toVector().size(), in gcc run-time that
basically does offset pointers last-first so it doesn't iterate.

So looping through a list of lists to get each size is O(n).


From: Jordan Brown [mailto:openscad@jordan.maileater.net]
Sent: Mon, 21 Feb 2022 14:37
To: OpenSCAD general discussion; Bob Ewart
Subject: [OpenSCAD] Re: Determining the dimensions of an array

Do be aware that OpenSCAD does not have 2D arrays or 3D arrays.

It has lists, and each list element may be different.  Yes, if you have a
list of N elements, each of which is a list of M elements, that's a lot
like an NxM array.

But this is equally legal:

[

 1,

 [2, 3],

 [

     [4,5,6],

     [7,8,9]

 ]

]

along with other combinations that are arbitrarily more complex.

If you know that your list and its elements form a simple rectangular
array then you can answer questions about dimensions and dimensionality,
but in the general case it's hard to figure out what the questions even
mean.

http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient Virus-free.
www.avg.com
http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient
<#m_1211036580186883035_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>


OpenSCAD mailing list
To unsubscribe send an email to discuss-leave@lists.openscad.org

Well, in this case, the constants dominate over the asymptotic performance, which is often the case in OpenSCAD, it seems. Running list_shape on a 1000x1000 matrix takes 0.96 seconds, which is pretty darn slow. In comparison, running is_matrix() takes 0.043 seconds, so it's 22 times faster in this case. Once you know it has matrix shape you can then do len(M) and len(M[0]) to get dimensions. What is_matrix does is to construct a list of zeros whose length matches the length of M[0] and then it checks if each entry in M satisfies M[i]*0 == list_of_zeros. This is a little different than using list_shape for the same test because is_matrix will only return true on numeric inputs. The is_matrix() function is much faster because it doesn't use recursion and it relies on OpenSCAD to check an entire row at a time, so the list comprehension is only across on dimension of the data structure. On Mon, Feb 21, 2022 at 2:18 AM MichaelAtOz <oz.at.michael@gmail.com> wrote: > Re performance, I wonder whether OpenSCAD maintains a list size as part of > the variables (internal) structure, or if len(list) runs through the list > to count them. > > > > I went and looked, it uses toVector().size(), in gcc run-time that > basically does offset pointers last-first so it doesn't iterate. > > So looping through a list of lists to get each size is O(n). > > > ------------------------------ > > *From:* Jordan Brown [mailto:openscad@jordan.maileater.net] > *Sent:* Mon, 21 Feb 2022 14:37 > *To:* OpenSCAD general discussion; Bob Ewart > *Subject:* [OpenSCAD] Re: Determining the dimensions of an array > > > > Do be aware that OpenSCAD does not have 2D arrays or 3D arrays. > > It has lists, and each list element may be different. Yes, if you have a > list of N elements, each of which is a list of M elements, that's a lot > like an NxM array. > > But this is equally legal: > > [ > > 1, > > [2, 3], > > [ > > [4,5,6], > > [7,8,9] > > ] > > ] > > along with other combinations that are arbitrarily more complex. > > If you know that your list and its elements form a simple rectangular > array then you can answer questions about dimensions and dimensionality, > but in the general case it's hard to figure out what the questions even > mean. > > > > > > > > <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> Virus-free. > www.avg.com > <http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> > <#m_1211036580186883035_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> > _______________________________________________ > OpenSCAD mailing list > To unsubscribe send an email to discuss-leave@lists.openscad.org >