Array size and calculations
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Array size and calculations

  1. #1
    Join Date
    Mar 2017
    Posts
    4

    Array size and calculations

    Hello everyone. This is my first post here..

    I am a beginner in C++ and have a question..

    Is a 100x100x100 array much much more complicated and calculation intensive than a say, 5x5x5 array, even though we will only have only few, same number of used elements in each ? For example assume each array will have only 2-3 used elements at any given time, even the 100x100 array.

    How about if most array elements were used ?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,587

    Re: Array size and calculations

    The amount of calculation required to access an element depends upon the number of dimensions of the array - not the number of elements in each dimension. The amount of memory required to hold the array is a function of both the number of dimensions and the number of elements in the dimensions. Assuming an array of int with an int using 4 bytes, a 100x100x100 matrix would require about 4Mb of memory whereas a 5x5x5 would require about 500 bytes.

    The size of the array and the calculation required to access an element also does not depend upon how many array elements are actually used - there is no distinction between a used and unused element.

    The size of an array is fixed at compile time. If the size is not known until run-time then other containers such as a vector may be more appropriate.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  3. #3
    Join Date
    Oct 2008
    Posts
    1,435

    Re: Array size and calculations

    Quote Originally Posted by 2kaud View Post
    The size of the array and the calculation required to access an element also does not depend upon how many array elements are actually used - there is no distinction between a used and unused element.
    this is not quite true ( assuming I understood what we mean by 'used' ), memory locality effects can cause slowdowns of orders of magnitudes ... so, generally speaking, keeping the data a core is working with as small and compact as possible ( to minimize cache hits, etc.. ) and as distant as posssible from other cores 'hot' data ( to avoid false sharing, etc... ) is always a good idea ...

    moreover, the compiler may take a decision on the specific addressing instruction used based on the array size as well, so, strictly speaking, they're not equivalent anyway
    Last edited by superbonzo; March 7th, 2017 at 09:58 AM.

  4. #4
    Join Date
    Feb 2017
    Posts
    111

    Re: Array size and calculations

    Quote Originally Posted by Ketanco View Post
    assume each array will have only 2-3 used elements at any given time
    To use just a few of the elements in a big 3-dimensional array may not be the best option.

    It may pay off to just pretend there is an array and instead associate the 2-3 elements that are currently in use with the [i,j,k] coordinates each would have if they were indeed stored in a 3d-array. The [i,j,k] coordinates can instead be viewed as a position in 3d space.
    Last edited by wolle; March 8th, 2017 at 09:51 AM.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,587

    Re: Array size and calculations

    Quote Originally Posted by superbonzo View Post
    this is not quite true ( assuming I understood what we mean by 'used' ), memory locality effects can cause slowdowns of orders of magnitudes ... so, generally speaking, keeping the data a core is working with as small and compact as possible ( to minimize cache hits, etc.. ) and as distant as posssible from other cores 'hot' data ( to avoid false sharing, etc... ) is always a good idea ...

    moreover, the compiler may take a decision on the specific addressing instruction used based on the array size as well, so, strictly speaking, they're not equivalent anyway
    Yeah, I was being too simplistic!
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,587

    Re: Array size and calculations

    Quote Originally Posted by wolle View Post
    To use just a few of the elements in a big 3-dimensional array may not be the best option.

    It may pay off to just pretend there is an array and instead associate the 2-3 elements that are currently in use with the [i,j,k] coordinates each would have if they were indeed stored in a 3d-array.
    For sparse arrays, there's other underlying data structures that could be considered rather than an array. Have a look at http://forums.codeguru.com/showthrea...nsional-arrays and post #7 onwards. This was for a many-dimensional array (with the example at 5) but is easy to extend to smaller/larger dimensions. This particular example obviously isn't as fast for access as using an array but depending upon the requirements something similar may be worth considering.
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017.2

  7. #7
    Join Date
    Mar 2017
    Posts
    4

    Re: Array size and calculations

    i asked this questions for making something in 3d space, to track location of moving points in 3d space, in relation to each other. i want to measure their distance and angle with respect to each other. that is why i said only few location even in 100x100 array will be used. as i said i am beginner in C++. may be there will be much better ways?

  8. #8
    Join Date
    Feb 2017
    Posts
    111

    Re: Array size and calculations

    Quote Originally Posted by Ketanco View Post
    i asked this questions for making something in 3d space, to track location of moving points in 3d space
    I suspected something like that and that's why I replied the way I did in #4.

    In C++ it's common to group data that belong together in units called classes or structs (which have minor differences). This is a very simple example:

    Code:
    struct Position {  // a position 
       int i,j,k; //3 integer coordinates
    };
    
    struct Element { // an element
       int id; // id number to tell different elements apart
       Position pos; // the position of an element in space
    };
    
    Element elem; // declares storage for one element called elem
    
         // declares an array called manyelem to store many elements
    std::vector<Element> manyelem; // (vector is a commonly used standard array that can grow/shrink as needed)
    Last edited by wolle; March 10th, 2017 at 12:11 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)